Ejemplo n.º 1
0
    $d = 10;
    $d = func($a, $b);
    exit;
}
if ($which == 1) {
    /**
     * 返回引用示例1
     */
    class foo
    {
        public $value = 20;
        function &getValue()
        {
            return $this->value;
        }
    }
    $obj = new foo();
    $value =& $obj->getValue();
    // test 2:
    $obj->value = 30;
    echo $value;
    exit;
    // test 1:
    $value = 30;
    echo $obj->value;
    exit;
}
$a = '100';
$b =& $a;
$b = 200;
echo $a;
Ejemplo n.º 2
0
//1
echo $b . '<br/>';
l($b);
//7
echo $b . '<br/>';
class foo
{
    public $v = 42;
    public function &getValue()
    {
        //返回的引用
        return $this->v;
    }
}
$o = new foo();
$m =& $o->getValue();
//引用的绑定
$o->v = 2;
// $m =2;
echo $m . '<br/>';
// 注意:
// 如果试图通过return ($this->v) 这种形式来返回,会不起作用并报错。
// 因为这是试图返回一个表达式的结果而不是一个引用的变量。
/***
pointer |value|variable
1       |NULL |
2		|NULL |
3		|NULL |
4		|NULL |
5		|NULL |
Ejemplo n.º 3
0
foo($a);
echo $a . '<br />';
function &bar()
{
    $a = 3;
    return $a;
}
class foo
{
    public $value = 42;
    public $name = 'Andi';
    public function &getValue()
    {
        return $this->value;
    }
    public function &getName()
    {
        return $this->name;
    }
}
$obj = new foo();
$myValue1 =& $obj->getValue();
$myValue2 = $obj->getValue();
echo $myValue1 . ':' . $myValue2 . '<br />';
$obj->value = 2;
echo $myValue1 . ':' . $myValue2 . '<br />';
$myName1 =& $obj->getName();
$myName2 = $obj->getName();
echo $myName1 . ':' . $myName2 . '<br />';
$obj->name = 'Cooler Typ';
echo $myName1 . ':' . $myName2 . '<br />';
Ejemplo n.º 4
0
require_once '../../includes/header.php';
?>
<pre>
<?php 
//Do not use return-by-reference to increase performance
class foo
{
    public $value = 42;
    public function &getValue()
    {
        return $this->value;
    }
}
$obj = new foo();
$myValue =& $obj->getValue();
// $myValue is a reference to $obj->value, which is 42.
$obj->value = 2;
echo $myValue . "\n";
// prints the new value of $obj->value, i.e. 2.
//To use the returned reference, you must use reference assigment:
function &collector()
{
    static $collection = array();
    return $collection;
}
$collection =& collector();
//you must use reference assigment
$collection[] = 'foo';
var_dump($collection);
//To pass the returned reference to another function expecting a reference you can use this syntax: