echo a static varible

2017-03-24  本文已影响0人  张霸天

Some thing that may be obvious to the seasoned PHP programmer, but may surprise someone coming over from C++:


<?php
class Foo
{
$bar = 'Hi There';

public function Print(){
    echo $bar;
}
}
?>

Gives an error saying Print used undefined variable. One has to explicitly use (notice the use of<?php $this->bar ?>):


<?php
class Foo
{
$bar = 'Hi There';

public function Print(){
    echo this->$bar;
}
}
?>


<?php echo $this->bar; ?> refers to the class member, while using $barmeans using an uninitialized variable in the local context of the member function.

上一篇 下一篇

猜你喜欢

热点阅读