Why is my basic php class expecting an argument when I instantiate it?
this works, but returns this warning:
Warning: Missing argument 1 for myClass::myClass()
I read in to this, and seems that the constructor is e开发者_开发知识库xpecting a value, so by adding:
function myClass($input='')
the warning is removed, but this seems so unnecessary?
could someone enlighten me as to why it's required to define a value to prevent that warning?
thanks for any pointers
You are using a method (functions in objects are called methods) that is the same name as the class. That is called the constructor, it has a special meaning in OOP.
The constructor is never called separately, but is the method that gets called automatically when you initialize the object. Any parameters that method has, you append to the new classname
statement.
$test = new myClass(123);
also, the constructor must never return a value. It is used only to do things while initializing the class, e.g. storing parameters. Any returned values will be lost, as the result of new myClass
is always the initialized object.
If you are just looking to create a method inside your class to return some text, then you need to change the method's name. This would work:
<?php
class myClass {
var $input;
var $output;
function someOtherFunctionName($input) {
$output = 'You entered: ' . $input;
return $output;
}
}
$test = new myClass;
echo $test->someOtherFunctionName(123);
?>
If a constructor is indeed what you want to use, then note that since PHP 5, it is standard practice to use __construct()
instead of creating a method with the same name as the class:
<?php
class myClass {
var $input;
var $output;
function __construct($input) {
$this->input = $input; // This is a valid thing to do in a constructor
}
}
More on constructors and destructors in PHP 5 in the manual.
Because in these class myClass is the constructor, so it needs to get a variable in construction time, like this
$test = new myClass(123);
you could try setting the function like a normal method instead
class myClass { var $input; var $output; function myClass(){} function setInput($input) { $output = 'You entered: ' . $input; return $output; } } $test = new myClass; echo $test->setInput(123);
The function you defined, myClass
, is your constructor. That is the old php4 syntax which is still valid in php5.
So when you generate an object, the function is run automatically and as you are not sending a value when you instantiate the object, you get a warning.
You should either use:
$test = new myClass(123);
or name the function in your class differently if you don´t want it to be the constructor.
php thinks that your "myClass" function is a constructor (because its name matches the class name) and calls it automatically after "new". If you don't want this, just rename the function:
class MyClass {
function foo($input) { ... }
...
}
$obj = new MyClass;
$obj->foo(bar);
If you want some parameters of methods/functions be optional you can specify their default values in the declaration like you did function myClass($input=NULL)
.
However you are learning PHP4 and it's no longer supported by the developers, have a look at PHP5.
精彩评论