Reputation: 141
I am learning php now using code academy website but some is not explained properly.
These are the conditions:
Cat
.$isAlive
ought to store the value true
and $numLegs
should contain the value 4.$name
property, which gets its value via the __construct()
or.meow()
, which returns "Meow meow".Cat
class, which has the $name
"CodeCat".meow()
method on this Cat and echo the result.This is the code created :
<!DOCTYPE html>
<html>
<head>
<title> Challenge Time! </title>
<link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
<p>
<?php
// Your code here
class Cat {
public $isAlive = true;
public $numLegs = 4;
public $name ;
public function __construct() {
$cat->name = $name;;
}
public function meow(){
return "Meow meow";
}
}
$cat = new Cat(true ,4 , CodeCat);
echo $cat->meow();
?>
</p>
</body>
</html>
Upvotes: 0
Views: 163
Reputation: 3002
In your code you have a constructor with no parameters, so $name
will be undefined. And when you want to create a new object Cat
you call it with 3 params, but you don't have a constructor like that.
What you want is to have a constructor with 1 param $name
and call it with that param like this:
<?php
// Your code here
class Cat {
public $isAlive = true;
public $numLegs = 4;
public $name ;
public function __construct($name) {
$this->name = $name;
}
public function meow(){
return $this->name;
}
}
$cat = new Cat('CodeCat'); //Like this you will set the name of the cat to CodeCat
echo $cat->meow(); //This will echo CodeCat
?>
Upvotes: 0
Reputation: 1964
There's three mistakes:
$cat
inside constructor instead of
$this
CodeCat
should be string 'CodeCat'
Working code should look something like this:
<?php
// Your code here
class Cat {
public $isAlive = true;
public $numLegs = 4;
public $name ;
public function __construct($isAlive,$numLegs,$name) {
$this->name = $name;
$this->isAlive = $isAlive;
$this->numLegs = $numLegs;
}
public function meow(){
return "Meow meow";
}
}
$cat = new Cat(true ,4 , 'CodeCat');
echo $cat->meow();
?>
Upvotes: 2