Closed Thread Icon

Topic awaiting preservation: class, inheritance, polymorphism ect Pages that link to <a href="https://ozoneasylum.com/backlink?for=26793" title="Pages that link to Topic awaiting preservation: class, inheritance, polymorphism ect" rel="nofollow" >Topic awaiting preservation: class, inheritance, polymorphism ect\

 
Author Thread
Hustluz
Nervous Wreck (II) Inmate

From:
Insane since: Jun 2003

posted 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();
?>



the error message is:

Warning: Missing argument 1 for Animal::Animal() on line 11

H][RO
Bipolar (III) Inmate

From: Australia
Insane since: Oct 2002

posted 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.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted 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>";
	}
}



This should allow you to create a new Animal (or subclass of animal) without specifying the color



.:[ Never resist a perfect moment ]:.

« BackwardsOnwards »

Show Forum Drop Down Menu