开发者

Class variables messing up [closed]

开发者 https://www.devze.com 2023-01-19 09:54 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.

consider this code :

Class Controller_xyz extends Controller
{
    protected $res=' ';
    public function action_reg()
    {
         $this->res="bla开发者_C百科h";
         $x="blah"
         echo $this->res;
         echo $x;
    }
}

output :

b
blah

why am I not able to change class variable?


You made a typo @

$x="blah" (missing ;)

Working code: http://ideone.com/jmICU

(Controller stripped out)

Please let me know if my answer has worked.


It is NOT class variable but instance variable if you define/access it this way.

Most of the time in PHP, i forget including $ in variable name. Try as

$this->$res = "blah";

0

精彩评论

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