티스토리 뷰

웹개발/Php

PHP 참조 반환하기

yaku 2010. 12. 20. 10:38


참조 반환하기

참조에 의한 반환하기는 참조에 연결되어 있는 변수를 찾는 함수를 사용할 때 유용합니다.
참조에 의한 반환을 성능을 증가시키기 위해 사용하지 마십시오. 엔진이 스스로 최적화해 줍니다.
참조를 반환하는건 기술적으로 할 필요가 있을 경우에만 하십시오.
참조를 반환하려면, 다음 문법을 사용합니다.

<?php
class foo {
    public 
$value 42;

    public function &
getValue() {
        
retrun $this->value;
    }
}

$obj = new foo;
$myValue = &$obj->getValue(); // $myValue는 $obj->value의 참조로, 42입니다.
$obj->value 2;
echo 
$myValue;                // $obj->value의 새 값, 즉 2를 출력합니다.
?>



위 예제 코드는 함수 getvalue에 의해 반환하는 객체의 속성을 설정하지만, 참조 문법을 사용하지 않을 때와 같은 복사가 아니다.

Note: 인수 전달과 달리, 여기서는 두 군데에서 &을 사용해야 한다 - 보통의 복사가 아니라, 참조에 의해 반환하는 것을 가리킬 때, 그리고 일반적인 지정이 아닌 참조 연결이라는 것을 가리킬때, $myValue에 대해 사용해야 한다.
Note: 다음 문법으로 함수에서 참조를 반환하려 할 수 있습니다: return ($this->value); 이는 작동하지 않습니다. 변수가 아닌 표현식을 참조로 반환하려고 하는 일이기 때문입니다. 함수에서는 변수만을 참조로 반환할 수 있습니다 - 다른 것은 안됩니다. PHP4의 PHP 4.4.0부터, PHP5의 PHP 5.1.0부터 코드가 동적 표현식이나 new 연산자의 결과를 반환하려 하면, E_NOTICE 오류가 발생합니다.


<?php

    function &func(){
        static $static = 0;
        $static++;
        return $static;
    }

    $var1 =& func();
    echo "var1:", $var1; // 1
    func();
    func();
    echo "var1:", $var1; // 3
    $var2 = func(); // assignment without the &
    echo "var2:", $var2; // 4
    func();
    func();
    echo "var1:", $var1; // 6
    echo "var2:", $var2; // still 4

?>


<?php

    function &func(){
        static $static = 0;
        $static++;
        return $static;
    }

    $var =& func();
    echo $var; // 1
    func();
    func();
    func();
    func();
    echo $var; // 5

?>


<?php

class Foo {

    protected $bar;

    public function __construct() {
        $this->bar = new Bar();

        print "Foo\n";
    }   
   
    public function getBar() {
        return $this->bar;
    }
}

class Bar {

    public function __construct() {
        print "Bar\n";
    }
   
    public function helloWorld() {
        print "Hello World\n";
    }
}

function test() {
    return new Foo();
}

test()->getBar()->helloWorld();

?>

<?php

class some_class
{
    function    & func_b ()
    {
       $this->some_var = 2;
       return $this->some_var;
    }
   
    function    func_a (& $param)
    {
       # $param is 1 here
       $param = $this->func_b();
       # $param is 2 here
    }
}

$var = 1;

$object = new some_class();
$object->func_a($var);

print $var; // this prints two
$var = 5;
print $object->some_var;
// $object->some_var  is still 2, because $var is not bound to it
// (as the original post by zayfod claims) and therefore has no
// effect on it
?>


<?php

class TestClass
{
    var $thisVar = 0;

    function TestClass($value)
    {
        $this->thisVar = $value;
    }

    function &getTestClass($value)
    {
        static $classes;

        if (!isset($classes[$value]))
        {
            $classes[$value] = new TestClass($value);
        }

        return $classes[$value];
    }
}

echo "<pre>";

echo "Getting class1 with a value of 432\n";
$class1 =& TestClass::getTestClass(432);
echo "Value is: " . $class1->thisVar . "\n";

echo "Getting class2 with a value of 342\n";
$class2 =& TestClass::getTestClass(342);
echo "Value is: " . $class2->thisVar . "\n";

echo "Getting class3 with the same value of 432\n";
$class3 =& TestClass::getTestClass(432);
echo "Value is: " . $class3->thisVar . "\n";

echo "Changing the value of class1 to 3425, which should also change class3\n";
$class1->thisVar = 3425;

echo "Now checking value of class3: " . $class3->thisVar . "\n";

?>

Which outputs:

Getting class1 with a value of 432
Value is: 432
Getting class2 with a value of 342
Value is: 342
Getting class3 with the same value of 432
Value is: 432
Changing the value of class1 to 3425, which should also change class3
Now checking value of class3: 3425


 

'웹개발 > Php' 카테고리의 다른 글

PHP 참조 (PHP References) - 2 (고급편)  (0) 2010.12.20
PHP 최적화 40가지  (0) 2010.12.20
레퍼런스 함수  (0) 2010.12.18
XDEBUG PROFILE  (0) 2010.12.15
PHP 매직 메소드  (0) 2010.11.25
댓글
D-DAY
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함