Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]klasa statyczna i adapter
tojalogin
post 30.04.2014, 11:08:14
Post #1





Grupa: Zarejestrowani
Postów: 1
Pomógł: 0
Dołączył: 30.04.2014

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


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?

Ten post edytował tojalogin 30.04.2014, 11:08:51
Go to the top of the page
+Quote Post
styryl
post 30.04.2014, 11:14:42
Post #2





Grupa: Zarejestrowani
Postów: 223
Pomógł: 27
Dołączył: 16.04.2008
Skąd: Bakutilu

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


  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()
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 20.06.2025 - 11:03