This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Objects in PHP Forum

View Course » View Exercise

198 points
6f8bcc313aa9b25f80d84edc86c6d488?s=140&d=retro
Submitted by
JoeNied
almost 4 years ago

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


1 vote

permalink

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.

3108 points
5154c6cf115b457ac9000021_667301409
Submitted by
boring12345
almost 4 years ago

2 Comments

6f8bcc313aa9b25f80d84edc86c6d488?s=140&d=retro JoeNied almost 4 years ago

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?

5154c6cf115b457ac9000021_667301409 boring12345 almost 4 years ago

$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. :-)


1 vote

permalink

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.

823 points
51ab61c371a8591877004ab8_842077421
Submitted by
sdny
almost 4 years ago

10 Comments

5154c6cf115b457ac9000021_667301409 boring12345 almost 4 years ago

What is the thing you don't completely understand? I'll try my best to explain it using more/better examples. :-)

51ab61c371a8591877004ab8_842077421 sdny almost 4 years ago

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.

5154c6cf115b457ac9000021_667301409 boring12345 almost 4 years ago

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.

51ab61c371a8591877004ab8_842077421 sdny almost 4 years ago

Awesome, I will take a look at that. Many thanks!

5154c6cf115b457ac9000021_667301409 boring12345 almost 4 years ago

Let me know, whether it helps. :-)

51ab61c371a8591877004ab8_842077421 sdny almost 4 years ago

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. :)

5154c6cf115b457ac9000021_667301409 boring12345 almost 4 years ago

Yes, the official manual is great. :-) If you search a place to practise, you also can write code online here: http://writecodeonline.com/php/

51ab61c371a8591877004ab8_842077421 sdny almost 4 years ago

Wow, fantastic! Many thanks! Your advice invaluable. :)

5154c6cf115b457ac9000021_667301409 boring12345 almost 4 years ago

You are welcome! :-) Feel free to ask, if you have further questions concerning PHP.

51ab61c371a8591877004ab8_842077421 sdny almost 4 years ago

I will make sure I will.