Forum und email

ייחוס שנכתב לבנאים

כתיבת ייחוס לבנאים עלול להוביל לתוצאות מבלבלות. חלק זה במדריך עוזר לך להמנע מהבעיות שיתכנו.

class Foo
{
    function Foo($name)
    {
        // create a reference inside the global array $globalref
        global $globalref;
        $globalref[] = &$this;
        // set name to passed value
        $this->setName($name);
        // and put it out
        $this->echoName();
    }

    function echoName()
    {
        echo "<br>",$this->name;
    }
    
    function setName($name)
    {
        $this->name = $name;
    }
}

בדיקה אם יש הבדל בין $bar1 שיוצר תוך שימוש בעותק של האופרטור = ושל $bar2 שנוצר תוך שימוש באופרטור הייחוס =& ...

$bar1 = new Foo('set in constructor');
$bar1->echoName();
$globalref[0]->echoName();

/* output:
set in constructor
set in constructor
set in constructor */

$bar2 =& new Foo('set in constructor');
$bar2->echoName();
$globalref[1]->echoName();

/* output:
set in constructor
set in constructor
set in constructor */

נראה שאין כל הבדל, אבל למעשה יש הבדל משמעותי מאוש אחד: $bar1 ו $globalref[0] הם לא מיוחסים, הם לא אותו משתנה. זאת בגלל ש "new" לא מחזיר יחוס בברירת מחדל, במקום זאת הוא מחזיר עותק.

Note: אין איבוד של ביצועים (מאחר ו PHP 4 ומעלה משתמשים במיספור ליחוס) ומוחזרים עותקים במקום ייחוס. מצד שני זה לעתים קרובות עדיף פשוט לעבוד עם עותקים במקום עם "ייחוסים", בגלל שיצירת "ייחסוים" לוקחת זמן מה כשיצירת עותקים וירטואלים אינה לוקחת זמן כלשהו (כל עוד אף אחד מהם הוא לא מערך גדול או אובייקט and ושאחד מהם משתנה ואחר(ים) לאחר מכן, במקר כזה זה יהיה חכם להשתמש בייחוסים לשנות את כולם בו זמנית).

להוכיח את מה שנכתב למעלה יש לבחון את הקוד הבא:
// now we will change the name. what do you expect?
// you could expect that both $bar1 and $globalref[0] change their names...
$bar1->setName('set from outside');

// as mentioned before this is not the case.
$bar1->echoName();
$globalref[0]->echoName();

/* output:
set from outside
set in constructor */

// let us see what is different with $bar2 and $globalref[1]
$bar2->setName('set from outside');

// luckily they are not only equal, they are the same variable
// thus $bar2->name and $globalref[1]->name are the same too
$bar2->echoName();
$globalref[1]->echoName();

/* output:
set from outside
set from outside */

לבסוף עוד דוגמא, מומלץ לנסות להבין אותה.

class A
{
    function A($i)
    {
        $this->value = $i;
        // try to figure out why we do not need a reference here
        $this->b = new B($this);
    }

    function createRef()
    {
        $this->c = new B($this);
    }

    function echoValue()
    {
        echo "<br>","class ",get_class($this),': ',$this->value;
    }
}


class B
{
    function B(&$a)
    {
        $this->a = &$a;
    }

    function echoValue()
    {
        echo "<br>","class ",get_class($this),': ',$this->a->value;
    }
}

// try to undestand why using a simple copy here would yield
// in an undesired result in the *-marked line
$a =& new A(10);
$a->createRef();

$a->echoValue();
$a->b->echoValue();
$a->c->echoValue();

$a->value = 11;

$a->echoValue();
$a->b->echoValue(); // *
$a->c->echoValue();

/*
output:
class A: 10
class B: 10
class B: 10
class A: 11
class B: 11
class B: 11
*/