PHP5 nesnelerine başvuru tarafından geçirilir?

6 Cevap

Ben bu konuda herhangi bir tutarlı bilgi almak için görünmüyor olabilir. Farklı kaynaklar farklı şeyler ve saygıdeğer php.net kendisi (appears) açıkça bu devlet için değil söylemek görünür - İtiraf etmeliyim, ancak ben sadece bir göz vardı.

Ben 'ağır' nesneler etrafında geçirerek durumlarda, ben referans olarak geçmesi gerekiyor, ama ben yazmaya devam etmek istemiyorum:

function foo(TypeName& $obj)

Ben sadece paçayı eğer

function foo(TypeName $obj)

Yani standart ne diyor?

6 Cevap

Nesneler geçti (ve atanmış) referans edilmiştir. Operatörün adresini kullanmaya gerek yok.

Verilen ne yazdığınız bir basitleştirme, ancak amacınıza uygun olacaktır. documentation şöyle:

One of the key-points of PHP5 OOP that is often mentioned is that "objects are passed by references by default". This is not completely true. This section rectifies that general thought using some examples.

A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

Daha ayrıntılı bir açıklama (oversimplification yanı sıra tanımlayıcı açıklıyor) kontrol için this answer.

PHP 5 itibariyle, tüm nesneleri geçirilir ve referans tarafından atanır.

PHP 4, sen hala referans olarak geçirilen nesneleri istediğiniz yere açıkça & operatörünü kullanarak, belirtmeniz gerekir.

Kimden PHP manual:

You can pass a variable by reference to a function so the function can modify the variable. The syntax is as follows:

<?php
function foo(&$var)
{
    $var++;
}

$a=5;
foo($a);
// $a is 6 here
?>

Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);.

Ve gelen What's New in PHP5:

In PHP 5, the infrastructure of the object model was rewritten to work with object handles. Unless you explicitly clone an object by using the clone keyword you will never create behind the scene duplicates of your objects. In PHP 5, there is neither a need to pass objects by reference nor assigning them by reference

$ Var bir sınıfın bir örneği olmayabilir Yani bu nedenle function foo(&$var) sözdizimini kullanmak için gereken tek zamandır.

Kısa cevap yes. Dan Objects and references:

One of the key-points of PHP5 OOP that is often mentioned is that "objects are passed by references by default". This is not completely true. This section rectifies that general thought using some examples.

A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

Önemli açıkça işlev çağrısında clone anahtar kelime kullanın sürece sizi endişelendiriyor durumda, bir nesnenin bir kopyasını yapmak gibi asla olmasıdır. Bir takma ad veya tanımlayıcı bu gerçeği değiştirmez olsun.

Evet PHP5'ta yılından itibaren, nesneler başvuruya göre iletilir. Açıkça bunu gerek yok.

http://www.php.net/manual/en/migration5.oop.php

PHP 5 yeni Nesne Modeli var. Nesnelerin PHP'nin kullanımı tamamen daha iyi performans ve daha fazla özellik için izin, yeniden yazılmıştır. PHP'nin önceki sürümlerinde, nesneleri (örneğin tamsayılar ve yaylılar için) ilkel türleri gibi ele alınmıştır. Bu yöntemin dezavantajı değişken atanmış veya bir yönteme bir parametre olarak kabul edildiğinde semantik bütün nesne kopyalanmış olmasıydı. Yeni yaklaşımda, nesneler tanıtıcı tarafından başvurulan ve değerine göre (tek bir nesnenin tanımlayıcı olarak bir sap düşünebilirsiniz) değil.

Bir nesnenin değeri değeri tarafından geçirilir, biraz daha kesin gibi görünüyor, ama bir nesnenin kendisi değer bir göstericidir. Bu sadece bir başvuru ileterek farklıdır.

http://www.php.net/manual/en/language.oop5.references.php listelenen örnek güzel On. Ilk sette, a = $ null; $ a sadece bir gösterici oldu beri b $ etkilemez. İkinci sette, $ c = null; $ d $ c bir referans beri de null olarak $ d olur.

<?php
class A {
    public $foo = 1;
}  

$a = new A;
$b = $a;
$a->foo = 2;
$a = NULL;
echo $b->foo."\n"; // 2

$c = new A;
$d = &$c;
$c->foo = 2;
$c = NULL;
echo $d->foo."\n"; // Notice:  Trying to get property of non-object...
?>