开发者

Return function pointing to local var in php

开发者 https://www.devze.com 2023-01-28 01:24 出处:网络
class Person{ var $name = \"Omer\"; functi开发者_开发技巧on get_name(){ return $this->name;//Why not $this->$name ?
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

精彩评论

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