This forum is now read-only. Please use our new forums! Go to forums
"Undefined variable: name:" but i already defined it in __construct
This is on Lession 7/10 I get an error saying undefined varible name but I don’t udnerstand isn’t it already defined in my constructor method?
<?php
class Dog {
public $numLegs = 4;
public $name;
public function __construct($name) {
$this->name = $name;
}
public function bark() {
return "bark bark bark";
}
public function greet() {
return "hello this dog's name is " . $name;
}
}
$onedog = new Dog("terry");
$twodog = new Dog("wen");
echo $onedog->greet();
?>
After clicking to try to go on to the next lesson it gives this code a pass. Just stilling getting an error in the preview window.
Answer 525aefea548c35c5f100970c
Hello,
to access a property you always need a $something->property
and as we want to access the $name
property within the same, i.e. $this
class we have to use $this->name
to access the property in the greet()
method.
Answer 525fffbcf10c6042cd000719
I was actually wondering the same thing. I know that you, boring12345, already explained it. Have to admit I still don’t “get it” though.
10 comments
What is the thing you don’t completely understand? I’ll try my best to explain it using more/better examples. :-)
I guess I don’t quite grasp the concept of $this yet and how it works. I kind of get it, and also kind of not.. haha. I guess I’ll simply repeat the course to get more familiar with the concepts.
You also could have a look at this tutorial: http://net.tutsplus.com/tutorials/php/object-oriented-php-for-beginners/ It says about $this
: Note — OOP allows objects to reference themselves using $this. When working within a method, use $this in the same way you would use the object name outside the class.
Awesome, I will take a look at that. Many thanks!
Let me know, whether it helps. :-)
Checked out your link and did some additional research, and I like to believe I get it now. Currently working through the php.net manual to get additional training. :)
Yes, the official manual is great. :-) If you search a place to practise, you also can write code online here: http://writecodeonline.com/php/
Wow, fantastic! Many thanks! Your advice invaluable. :)
You are welcome! :-) Feel free to ask, if you have further questions concerning PHP.
I will make sure I will.
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
2 comments
So do I also need to pass in $name variable to the greet function? Or Since it used the construct function it uses that name? Is $this referring to the current object?
$this
refers the the class from which you call$this
. And as you try to access a property of the same class, you don’t need to pass in$name
. You can simply access$this->name
in the method, just as you did it in earlier exercises. :-)