php引用返回function & fun();学习笔记
php引用是一个很有学问的东西了,下文我们来介绍php引用返回function & fun();学习笔记,希望例子可以帮助到各位.
例子代码如下:
<?php class talker{ private $data = 'Hi'; public function & get(){ return $this->data; } public function out(){ echo $this->data; } } $aa = new talker(); $d = &$aa->get(); $aa->out(); $d = 'How'; $aa->out(); $d = 'Are'; $aa->out(); $d = 'You'; $aa->out(); //the output is "HiHowAreYou"
例子二,代码如下:
<?php class person{ private $name; function & getName(){ return $this->name; } } $p = new person(); //注person类中的getName无论前面加不加& 在此调用都不会报错;只不过这样调用没有任何意义 $name = & $p->getName(); $name = 'walker'; $name = $p->getName(); echo $name;//输出walker
补充:函数的引用返回,代码如下:
<?php function &test() { static $b=0;//申明一个静态变量 $b=$b+1; echo $b; return $b; } $a=test();//这条语句会输出 $b的值 为1 $a=5; $a=test();//这条语句会输出 $b的值 为2 $a=&test();//这条语句会输出 $b的值 为3 $a=5; $a=test();//这条语句会输出 $b的值 为6
教程地址:http://www.phprm.com/develop/fs9094.html
欢迎转载!但请带上文章地址^^