This is the way how we use pointer to access variable inside the class.
<?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"
4.对象的引用
<?php
class a{
var $abc="ABC";
}
$b=new a;
$c=$b;
echo $b->abc;//这里输出ABC
echo $c->abc;//这里输出ABC
$b->abc="DEF";
echo $c->abc;//这里输出DEF
?>
在php4中,对象的赋值是个拷贝过程,
如:$b=new a,其中new a产生的是一个匿名的a对象实例,而此时的$b是对这个匿名对象的拷贝。同理$c=$b,也是对$b内容的一个拷贝。所以在php4中,为了节省内存空间,$b=new a 一般会改成引用的模式,即 $b=& new a。
下面再来个 官方 提供的例子:
在php5中,你不需要额外添加什么东西就可到达“对象引用”的功能:
<?php
class foo{
protected $name;
function __construct($str){
$this->name = $str;
}
function __toString(){
return 'my name is "'. $this->name .'" and I live in "' . __CLASS__ . '".' . "\n";
}
function setName($str){
$this->name = $str;
}
}
class MasterOne{
protected $foo;
function __construct($f){
$this->foo = $f;
}
function __toString(){
return 'Master: ' . __CLASS__ . ' | foo: ' . $this->foo . "\n";
}
function setFooName($str){
$this->foo->setName( $str );
}
}
class MasterTwo{
protected $foo;
function __construct($f){
$this->foo = $f;
}
function __toString(){
return 'Master: ' . __CLASS__ . ' | foo: ' . $this->foo . "\n";
}
function setFooName($str){
$this->foo->setName( $str );
}
}
$bar = new foo('bar');
print("\n");
print("Only Created \$bar and printing \$bar\n");print( $bar );print("\n");print("Now \$baz is referenced to \$bar and printing \$bar and \$baz\n");$baz =& $bar;print( $bar );print("\n");print("Now Creating MasterOne and Two and passing \$bar to both constructors\n");$m1 = new MasterOne( $bar );$m2 = new MasterTwo( $bar );print( $m1 );print( $m2 );print("\n");print("Now changing value of \$bar and printing \$bar and \$baz\n");$bar->setName('baz');print( $bar );print( $baz );print("\n");print("Now printing again MasterOne and Two\n");print( $m1 );print( $m2 );print("\n");print("Now changing MasterTwo's foo name and printing again MasterOne and Two\n");$m2->setFooName( 'MasterTwo\'s Foo' );print( $m1 );print( $m2 );print("Also printing \$bar and \$baz\n");print( $bar );print( $baz );?>
输出:
Only Created $bar and printing $bar
my name is "bar" and I live in "foo".
Now $baz is referenced to $bar and printing $bar and $baz
my name is "bar" and I live in "foo".
Now Creating MasterOne and Two and passing $bar to both constructors
Master: MasterOne | foo: my name is "bar" and I live in "foo".
Master: MasterTwo | foo: my name is "bar" and I live in "foo".
Now changing value of $bar and printing $bar and $baz
my name is "baz" and I live in "foo".
my name is "baz" and I live in "foo".
Now printing again MasterOne and Two
Master: MasterOne | foo: my name is "baz" and I live in "foo".
Master: MasterTwo | foo: my name is "baz" and I live in "foo".
Now changing MasterTwo's foo name and printing again MasterOne and Two
Master: MasterOne | foo: my name is "MasterTwo's Foo" and I live in "foo".
Master: MasterTwo | foo: my name is "MasterTwo's Foo" and I live in "foo".
Also printing $bar and $baz
my name is "MasterTwo's Foo" and I live in "foo".
my name is "MasterTwo's Foo" and I live in "foo".
上个例子解析:
$bar = new foo('bar');
$m1 = new MasterOne( $bar );
$m2 = new MasterTwo( $bar );
如果在一个函数内部给一个声明为 global 的变量赋于一个引用,该引用只在函数内部可见。可以通过使用$GLOBALS数组避免这一点。 Example 在函数内引用全局变量
<?php
$var1 = "Example variable";
$var2 = "";
function global_references($use_globals)
{
global $var1, $var2;
if (!$use_globals) {
$var2 =& $var1; // visible only inside the function
} else {
$GLOBALS["var2"] =& $var1; // visible also in global context
}
}
global_references(false);
echo "var2 is set to '$var2'\n"; // var2 is set to ''
global_references(true);
echo "var2 is set to '$var2'\n"; // var2 is set to 'Example variable'
?>
把 global $var; 当成是 $var =& $GLOBALS['var']; 的简写。从而将其它引用赋给 $var 只改变了本地变量的引用。
Warning
如果在一个函数内部给一个声明为 global 的变量赋于一个引用,该引用只在函数内部可见。可以通过使用$GLOBALS数组避免这一点。 Example #2 在函数内引用全局变量
<?php
$var1 = "Example variable";
$var2 = "";
function global_references($use_globals)
{
global $var1, $var2;
if (!$use_globals) {
$var2 =& $var1; // visible only inside the function
} else {
$GLOBALS["var2"] =& $var1; // visible also in global context
}
}
global_references(false);
echo "var2 is set to '$var2'\n"; // var2 is set to ''
global_references(true);
echo "var2 is set to '$var2'\n"; // var2 is set to 'Example variable'
?>
把 global $var; 当成是 $var =& $GLOBALS['var']; 的简写。从而将其它引用赋给 $var 只改变了本地变量的引用。
Note:
如果在 foreach 语句中给一个具有引用的变量赋值,被引用的对象也被改变。 Example #3 引用与 foreach 语句
<?php
$ref = 0;
$row =& $ref;
foreach (array(1, 2, 3) as $row) {
// do something
}
echo $ref; // 3 - last element of the iterated array
?>
It appears that references can have side-effects. Below are two examples. Both are simply copying one array to another. In the second example, a reference is made to a value in the first arraybefore the copy. In the first example the value at index 0 points to two separate memory locations. In the second example, the value at index0 points to the same memory location.
I won't say this is a bug, because I don't know what the designed behavior of PHP is, but I don't think ANY developers would expect this behavior, so look out.
An example of where this could cause problems is if you do an array copyin a script and expect on type of behavior, but then later add a reference to a value in the array earlier in the script, and then find that the array copy behavior has unexpectedly changed.