Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Przesyłanie obiektów przez wartośc i refgerencję
amii
post
Post #1





Grupa: Zarejestrowani
Postów: 728
Pomógł: 76
Dołączył: 12.06.2009

Ostrzeżenie: (0%)
-----


W komentarzach na tej stronie -> http://www.php.net/manual/en/language.oop5.references.php mamy coś takiego. Z tego co rozumiem to:
1. Przesłanie zmiennych przez wartość powoduje utworzenie lokalnych kopii w obrębie funkcji w efekcie czego wartości pierwotnych zmiennych się nie zmieniają.
2. Przesłanie zmiennych przez referencję powoduje przesłanie do funkcji adresu pierwotnych zmiennych zatem w efekcie wartości się zmieniają po wyjściu z funkcji, gdyż modyfikowane jest to co się znajduje pod tymi adresami w pamięci.

  1. //The two are NOT meant to be the same.
  2. $c="King";
  3. $d="Pretender to the Throne";
  4. echo $c."\n"; // $c=="King"
  5. echo $d."\n"; // $d=="Pretender to the Throne"
  6. swapByValue($c, $d);
  7. echo $c."\n"; // $c=="King"
  8. echo $d."\n"; // $d=="Pretender to the Throne"
  9. swapByRef($c, $d);
  10. echo $c."\n"; // $c=="Pretender to the Throne"
  11. echo $d."\n"; // $d=="King"
  12.  
  13. function swapByValue($x, $y){
  14. $temp=$x;
  15. $x=$y;
  16. $y=$temp;
  17. //All this beautiful work will disappear
  18. //because it was done on COPIES of pointers.
  19. //The originals pointers still point as they did.
  20. }
  21.  
  22. function swapByRef(&$x, &$y){
  23. $temp=$x;
  24. $x=$y;
  25. $y=$temp;
  26. //Note the parameter list: now we switched 'em REAL good.
  27. }


To jest OK natomiast w manulau jest jednak coś takiego.

  1. class A {
  2. public $foo = 1;
  3. }
  4.  
  5.  
  6. $a = new A;
  7. $b = $a; // $a and $b are copies of the same identifier
  8. // ($a) = ($b) = <id>
  9. $b->foo = 2;
  10. echo $a->foo."\n"; //Wyświetla 2
  11.  
  12.  
  13.  
  14. $e = new A;
  15.  
  16. function foo($obj) {
  17. // ($obj) = ($e) = <id>
  18. $obj->foo = 2;
  19. }
  20.  
  21. foo($e);
  22. echo $e->foo."\n"; //Wyświetla 2


1. Dlaczego wyświetla 2 skoro $e był przesłany jako kopia ?
2. Dlaczego $a wyświetla 2 jeśli nie był przesłany przez referencję ?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Crozin
post
Post #2





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

Ostrzeżenie: (0%)
-----


Po pierwsze w PHP nie ma wskaźników, są jedynie referencje.

Co do obiektów i referencji w manualu jest to dobrze wytłumaczone:
Cytat
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.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 12.10.2025 - 11:25