Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]function __call - może ktoś wyjaśni
MESSIAH :)
post
Post #1





Grupa: Zarejestrowani
Postów: 249
Pomógł: 0
Dołączył: 22.12.2011

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


Szukam wyjaśnienia funkcji __call. Wiem że ona woła obiekty następne po niej i w sposób niejawny je może przeładować czyli edytować w locie. Znalazłem taki kod lecz nie za bardzo go rozumiem - nie wiem czy jest on poprawny:
  1.  
  2. class MethodTest {
  3.  
  4. public $xy = NULL;
  5.  
  6. public function __call($name, $arguments)
  7. {
  8. $arguments = (array) $arguments;
  9. $arguments['dsd'] = 'mt';
  10. $fname = 'pots'.ucfirst($name); /*dodaje przedrostek "pots" i podnosi pierwszą literę*/
  11. echo $arguments;
  12.  
  13. return $this->xy->$fname($arguments);
  14. }
  15. }
  16.  
  17. $obj = new MethodTest;
  18. $obj->runTest();

Mam pytanie dlaczego (array) jest w nawiasach zwracając tym samym NULL?

Wypluwa taki błąd:
Cytat
Notice: Array to string conversion in C:\xampp\htdocs\Untitled-2.php on line 11
Array
Fatal error: Call to a member function doRunTest() on null in C:\xampp\htdocs\Untitled-2.php on line 13


Ten post edytował MESSIAH :) 5.01.2015, 21:34:50
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Forti
post
Post #2





Grupa: Zarejestrowani
Postów: 655
Pomógł: 73
Dołączył: 2.05.2014

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


  1.  
  2. class MethodTest {
  3.  
  4. public $xy = NULL;
  5.  
  6. public function __call($name, $arguments)
  7. {
  8. $arguments = (array) $arguments;
  9. $arguments['dsd'] = 'mt';
  10. $fname = 'pots'.ucfirst($name); /*dodaje przedrostek "pots" i podnosi pierwszą literę*/
  11. echo $arguments;
  12.  
  13. return $this->xy->$fname($arguments);
  14. }
  15. }
  16.  
  17. $obj = new MethodTest;
  18. $obj->runTest();


Jeżeli się nie mylę...

_call jest wywoływana gdy wywolujesz nie istniejącą metodę. RunText() nie istnieje, więc wykonuje się __call, w którym zwracasz $this->xy, które jest NULL.

Czego nie rozumiesz?


edit:

  1. $arguments = (array) $arguments;


oznacza tyle, że $arguments musi być typu array. - mogę się mylić ;]

Ten post edytował Forti 5.01.2015, 21:35:13
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 - 04:53