Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> zwracanie referencji przez funkcję
flashdev
post
Post #1





Grupa: Zarejestrowani
Postów: 812
Pomógł: 117
Dołączył: 2.12.2008

Ostrzeżenie: (10%)
X----


  1. // test.php
  2. <?php
  3. class test{
  4. private $variable = 'default';
  5.  
  6. public function &getVariable1(){
  7. return $this -> variable;
  8. }
  9. public function getVariable2(){
  10. $ref = &$this -> variable;
  11. return $ref;
  12. }
  13. public function &getVariable3(){
  14. $ref = &$this -> variable;
  15. return $ref;
  16. }
  17.  
  18. public function __destruct() {
  19. echo $this -> variable;
  20. }
  21. }
  22. ?>


  1. <?php
  2.  
  3. include('class/test.php');
  4. $test = new test();
  5. $variable = $test -> getVariable1();
  6. $variable = 'new value';
  7. $variable = $test -> getVariable2();
  8. $variable = 'new value';
  9. $variable = $test -> getVariable3();
  10. $variable = 'new value';
  11.  
  12. ?>


Czy potrafi mi ktoś wytłumaczyć dlaczego żadna z tych funkcji nie zwraca referencji? Da się takie coś wogóle zrobić?
Go to the top of the page
+Quote Post
darko
post
Post #2





Grupa: Zarejestrowani
Postów: 2 885
Pomógł: 463
Dołączył: 3.10.2009
Skąd: Wrocław

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


Źle wywołujesz, zwróć uwagę na sposób wykorzystania (w przykładach) http://www.php.net/manual/pl/language.references.return.php

Czyli, np

  1. $variable = &$test -> getVariable1();
  2. $variable = 'new value';
  3. $variable = $test -> getVariable2();
  4. $variable = 'new value';
  5. $variable = &$test -> getVariable3();


Ten post edytował darko 8.12.2009, 00:17:05
Go to the top of the page
+Quote Post
flashdev
post
Post #3





Grupa: Zarejestrowani
Postów: 812
Pomógł: 117
Dołączył: 2.12.2008

Ostrzeżenie: (10%)
X----


Dzięki za pomoc. Już rozumiem gdzie tkwi błąd. Funkcja 1 oraz 3 zwracała referencję prawidłowo, tylko w linijakch 1, 3 oraz 5 kopiowałem obiekt na nowo.
Go to the top of the page
+Quote Post

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: 26.09.2025 - 02:43