开发者

Return function pointing to local var in php

class Person{

     var $name = "Omer";

     functi开发者_开发技巧on get_name(){
         return $this->name;//Why not $this->$name ?
     }
}

Thanks


If you use $this->$name it will actually look for a property in $this with the name of whatever $name is equal to. So, in your example, $this->$name would look for $this->Omer.


To illustrate what @Aaron has so eloquently answered, the following would compile:

class Person{
     var $name = "Omer";
     function get_name(){
         $varname = 'name';
         return $this->$varname;
     }
}
$Person = new Person;
echo $Person->get_name(); // output = Omer
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜