Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]klasa statyczna i adapter
Forum PHP.pl > Forum > Przedszkole
tojalogin
Zabieram się za adaptery i napotykam na błąd którego nie umiem przeskczyć. Czy w ogóle implementacja tego typu jest możliwa, aby klasa statyczna brała z kontruktora interfejs?

  1. $a= A::init(new A_Db);


  1. class A
  2. {
  3. protected static $instance = false;
  4. protected $adapter = null;
  5.  
  6. protected function __construct(\X\Interfaces\A $adapter){$this->adapter = $adapter;}
  7. protected function __clone(){}
  8.  
  9. public static function init()
  10. {
  11. if(!self::$instance instanceof self)
  12. {
  13. self::$instance = new self();
  14. }
  15.  
  16. return self::$instance;
  17. }// end function init()


Dostaję taki komunikat:
Cytat
Catchable fatal error: Argument 1 passed to X\lib\A::__construct() must implement interface X\Interfaces\A, none given, called in /var/www/A.php on line 27 and defined in /var/www/A.php on line 20


Jeśli odwołam się do klasy normalnie przez new A(); to zadziała, a jak zrobić aby było to przez jedną instancje?
styryl
  1. class A
  2. {
  3. protected static $instance = false;
  4. protected $adapter = null;
  5.  
  6. protected function __construct(\X\Interfaces\A $adapter){$this->adapter = $adapter;}
  7. protected function __clone(){}
  8.  
  9. public static function init( \X\Interfaces\A $adapter )
  10. {
  11. if(!self::$instance instanceof self)
  12. {
  13. self::$instance = new self( $adapter );
  14. }
  15.  
  16. return self::$instance;
  17. }// end function init()
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.