This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by JoeNied
over 10 years

"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

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.

points
Submitted by boring12345
over 10 years

2 comments

JoeNied over 10 years

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?

boring12345 over 10 years

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

Answer 525fffbcf10c6042cd000719

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.

points
Submitted by sdny
over 10 years

10 comments

boring12345 over 10 years

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

sdny over 10 years

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.

boring12345 over 10 years

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.

sdny over 10 years

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

boring12345 over 10 years

Let me know, whether it helps. :-)

sdny over 10 years

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

boring12345 over 10 years

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

sdny over 10 years

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

boring12345 over 10 years

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

sdny over 10 years

I will make sure I will.