Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> static problem
Dex1987
post
Post #1





Grupa: Zarejestrowani
Postów: 246
Pomógł: 0
Dołączył: 28.09.2004

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


Witam, mam taki oto problem:

mam klase engine:

  1. <?php
  2. // engine.php
  3.  
  4. class engine {
  5.  
  6.  static public $_templates;
  7.  
  8.  public function __construct () {
  9.  
  10. $this -> _templates ();
  11.  
  12. echo 'engine: ' . $this -> _templates . '<br>'; //to zwraca: Object id #2
  13.  }
  14.  
  15.  public function _templates () {
  16.  
  17.  $this -> _templates = new templates ();
  18.  }
  19. }
  20. ?>


oraz druga klase access

  1. <?php
  2. //access.php
  3.  
  4. class access extends engine {
  5.  
  6.  public function __construct () {
  7.  
  8.  parent::_templates ();
  9.  }
  10.  
  11.  public function register () {
  12.  
  13.  echo 'access: ' . $this -> _templates; //to zwraca: Object id #
  14. }
  15. }
  16. ?>


i teraz chciabym zeby w obu klasach bylo zwracane Object id #2. Probowalem kombinowac cos z self ale dalej bylo to samo, albo wogole nie dzialalo.

pozdrawiam dex

Ten post edytował Dex1987 22.10.2006, 23:40:00
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
Cysiaczek
post
Post #2





Grupa: Moderatorzy
Postów: 4 465
Pomógł: 137
Dołączył: 26.03.2004
Skąd: Gorzów Wlkp.




  1. <?php
  2. class access extends engine {
  3.  
  4.  public function __construct () {
  5.  
  6. // parent::_templates (); //tak sie TY odnosisz
  7.  parent::__construct(); //a jak zrobisz tak, to działa :)
  8.  }
  9. ?>


Jeśli w klasie dziedziczącej jest konstruktor, to wywołanie kontruktora klasy bazowej z klasy dziedziczącej musi być dokonane ręcznie. Mówiąc bardziej po ludzku: Jeśli masz w obu klasach konstruktory, to w dziedziczącej zawsze na początku wywołuj parent::__construct(); Wtedy masz pewność, że oiekty będą prawidłowo zainicjowane.

Pozdrawiam.

Ten post edytował Cysiaczek 23.10.2006, 10:26:06


--------------------
To think for yourself you must question authority and
learn how to put yourself in a state of vulnerable, open-mindedness;
chaotic, confused, vulnerability, to inform yourself.
Think for yourself. Question authority.
Go to the top of the page
+Quote Post
Dex1987
post
Post #3





Grupa: Zarejestrowani
Postów: 246
Pomógł: 0
Dołączył: 28.09.2004

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


dzieki, rozwiazalem troche inaczej ten prolem, ale Twoje rozwiazanie jest lepsze
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 Aktualny czas: 21.08.2025 - 18:34