Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: __call() != return ?
Forum PHP.pl > Forum > PHP > Object-oriented programming
bregovic
Mam w mojej klasie Db funkcje __call() ktora wywoluje funkcje z innego obiektu (Driver). problem polega na tym ze gdy uzywam np $this->Connect($arg) gdzie Connect jest metoda obiektu - tzn nie zawarta w aktualnej klasie, to metoda zostaje wykonana, lecz metoda ta nie moze nic zwrocic poprzez return!

W praktyce metoda __call() wyglada tak:
  1. <?php
  2. public function __call($function, $arg)
  3. {
  4. call_user_method_array($function, $this->driver, $arg);
  5. }
  6. ?>


Wywoluje ja w konstruktorze poprzez:
  1. <?php
  2. public function __construct($dsn)
  3.  
  4. {
  5. $this->ParseDsn($dsn);
  6. $this->LoadDriver();
  7. $this->driver = new Driver();
  8.  
  9. # tutaj wywoluje Connect: #
  10. if($this->Connect($this->dsn)==false)
  11. {
  12. throw new Exception ('Unable to connect to the specified database. Database said: \"'./*$this->GetError($this->conn)*/mysql_error().'\".', E_DB_CONN);
  13. }
  14.  
  15. }
  16. ?>


A metoda Connect w clasie Driver wyglada nastepujaco:
  1. <?php
  2. function Connect($dsn)
  3. {
  4. $db = mysql_connect($dsn['dbHost'], $dsn['dbUser'], $dsn['dbPass']) or die (mysql_error());
  5. mysql_select_db($dsn['dbName'], $db) or die (mysql_error());
  6. return $db;
  7. }
  8. ?>


gdy sprawdzam co zwrocilo $this->Connect($this->dsn) (poprzez var_dump" title="Zobacz w manualu PHP" target="_manual) otrzymuje przyjemne NULL... czy ktos moze cos zaradzic?
davidD
Cytat(bregovic @ 2004-08-10 23:03:59)
Mam w mojej klasie Db funkcje __call() ktora wywoluje funkcje z innego obiektu (Driver). problem polega na tym ze gdy uzywam np $this->Connect($arg) gdzie Connect jest metoda obiektu - tzn nie zawarta w aktualnej klasie, to metoda zostaje wykonana, lecz metoda ta nie moze nic zwrocic poprzez return!

W praktyce metoda __call() wyglada tak:
  1. <?php
  2. public function __call($function, $arg)
  3. {
  4. call_user_method_array($function, $this->driver, $arg);
  5. }
  6. ?>

Poprostu zrób tak:
  1. <?php
  2. public function __call($function, $arg)
  3. {
  4. return call_user_method_array($function, $this->driver, $arg);
  5. }
  6. ?>


...i wszystko powinno działać tongue.gif

Aha, tak btw. call_user_method_array jest niezalecane - lepiej używać call_user_func_array:

  1. <?php
  2.  
  3. call_user_func_array(array(&$object, $methodName), $args);
  4.  
  5. ?>
bregovic
Wielkie dzieki smile.gif
Alez ja musialem wczoraj byc zaspany biggrin.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.