Re: Help with classes (oop)
| From: | Chris Boget | Date: | Mon, 03 Feb 2003 19:45:13 +0000 |
| Subject: | Re: Help with classes (oop) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-134168@lists.php.net to get a copy of this message | ||
> Apparently it does not like the function name to be the same as the class
> name. So change one of them.
No, what's happening is that when you instantiate an class, it runs
(as a constructor) the function whose name is the same as the class.
So when you do this:
$first = new first;
it's automatically running the class' "first()" method. Now, because that
method
is defined as requiring parameters, you need to do this:
$first = new first( 35, "Chris" );
Alternately, you can define your function/method thusly:
first($age = 0, $name = '');
but that isn't going to accomplish anything other than getting rid of the errors.
You either want to create a constructor that initializes all the member variables
or not create a constructor at all.
This would be a better way of writing your class:
class first
{
var $age;
var $name;
function first()
{
$age = 0;
$name = '';
}
function setData( $age, $name )
{
$age = $age;
$name = $name;
}
function returnData()
{
$retval = '';
$retval = $this->age;
$retval .= $this->name;
return $retval;
}
}
Chris
(also)