Topic awaiting preservation: class, inheritance, polymorphism ect |
|
---|---|
Author | Thread |
Nervous Wreck (II) Inmate From: |
posted 10-05-2005 17:10
im trying to practice working with class, inheritance, polymorphism ect well anyway i get an error with my code: code: <? class Animal{ function Animal($colour){ $this->colour = $colour; } function report(){ return "This ".$this->colour." ".get_class($this)." has ".$this->legs." legs.<br>"; } } class Cat extends Animal{ var $legs = 4; } class Panther extends Cat{ var $colour = "black"; function Panther(){} } class Snake extends Animal{ var $legs = 0; } $tiddles = new Cat(); $tiddles->legs--; $moggy = new Panther(); $hissy = new Snake("brown"); echo $tiddles->report(); echo $moggy->report(); echo $hissy->report(); ?>
|
Bipolar (III) Inmate From: Australia |
posted 10-05-2005 17:40
the function with the same name as the class, "Animal" is the constructor, and it runs anytime you create an instance of the class. Since you have an argument in the brackets it expects you to pass one to it. So take $colour out of the brackets. |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 10-05-2005 20:48
Or you can override the constructor. code: class Animal{ function Animal() {} function Animal($colour){ $this->colour = $colour; } function report(){ return "This ".$this->colour." ".get_class($this)." has ".$this->legs." legs.<br>"; } }
|