potatoCode
July 18th, 2009, 02:15 AM
Hello php gurus
If a class has a method named after the class, is the function constructor?
If I have a base class as// Inheritance test
class Animal
{
function __construct($ref)
{
$this->animalName = $ref;
}
function eat()
{
printf("%s is eating<br />", $this->animalName);
}
protected $animalName;
}and I'd like to call the base's constructor in the derived//
class Panda extends Animal
{
function __construct($ref)
{
$this->Animal($ref); //works only if Animal($ref) is defined in the base
}
function eat()
{
printf("Big %s is eating<br />", $this->animalName);
return $this;
}
function yawn()
{
printf("Big %s is yawning<br />", $this->animalName);
}
}
What is the difference between __construct() and the function with the same class name?
Thanks for the help.
If a class has a method named after the class, is the function constructor?
If I have a base class as// Inheritance test
class Animal
{
function __construct($ref)
{
$this->animalName = $ref;
}
function eat()
{
printf("%s is eating<br />", $this->animalName);
}
protected $animalName;
}and I'd like to call the base's constructor in the derived//
class Panda extends Animal
{
function __construct($ref)
{
$this->Animal($ref); //works only if Animal($ref) is defined in the base
}
function eat()
{
printf("Big %s is eating<br />", $this->animalName);
return $this;
}
function yawn()
{
printf("Big %s is yawning<br />", $this->animalName);
}
}
What is the difference between __construct() and the function with the same class name?
Thanks for the help.