Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> nazwa klasy dziedziczącej
zmszaman
post
Post #1





Grupa: Nieautoryzowani
Postów: 10
Pomógł: 0
Dołączył: 29.08.2009

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


Witam!

Jak zrobić żeby zamiast Base wyswietlało mi sie T w poniższym kodzie? Oczywiście po klasie Base będzie dziedziczyło więcej klas więc odpada wpisanie na sztywno T

  1. class Base {
  2. static public function getInstance( ) {
  3. echo __CLASS__;
  4. }
  5. }
  6. class T extends Base {}
  7. T::getInstance();
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 6)
Ociu
post
Post #2





Grupa: Moderatorzy
Postów: 1 566
Pomógł: 37
Dołączył: 14.05.2003
Skąd: Kraków




  1. class Base {
  2. static public function getInstance( ) {
  3. echo __CLASS__;
  4. }
  5. }
  6. class T extends Base {
  7. static public function getInstance( ) {
  8. echo __CLASS__;
  9. }
  10. }
  11. T::getInstance();
  12. Base::getInstance();
Go to the top of the page
+Quote Post
zmszaman
post
Post #3





Grupa: Nieautoryzowani
Postów: 10
Pomógł: 0
Dołączył: 29.08.2009

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


A bez nadpisywania tej metody jest jakaś możliwość?
Go to the top of the page
+Quote Post
wookieb
post
Post #4





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




Nie, ponieważ
Cytat
Static references to the current class like self:: or __CLASS__ are resolved using the class in which the function belongs, as in where it was defined:

http://pl2.php.net/manual/pl/language.oop5...ic-bindings.php


--------------------
Go to the top of the page
+Quote Post
-=Peter=-
post
Post #5





Grupa: Zarejestrowani
Postów: 304
Pomógł: 51
Dołączył: 4.02.2005
Skąd: Kraków

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


W php 5.3 wprowadzono nowy operator static, przykładowo:

  1. <?php
  2.  
  3. class Base {
  4. static public function getInstance( ) {
  5. echo static::getClass();
  6. }
  7.  
  8. static public function getClass(){
  9. echo __CLASS__;
  10. }
  11. }
  12. class T extends Base {
  13. static public function getClass(){
  14. echo __CLASS__;
  15. }
  16. }
  17.  
  18. T::getInstance();

Ale i tak to na jedno wychodzi w tym przypadku ;]


--------------------
Go to the top of the page
+Quote Post
taktu
post
Post #6





Grupa: Zarejestrowani
Postów: 89
Pomógł: 7
Dołączył: 19.05.2008

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


Jeśli będziesz tworzył obiekt to get_class( $this );
http://www.php.net/manual/en/function.get-class.php

Jeżeli chcesz wywoływać statycznie to get_called_class(); od php 5.3.0
http://php.net/manual/en/function.get-called-class.php


--------------------
Go to the top of the page
+Quote Post
zmszaman
post
Post #7





Grupa: Nieautoryzowani
Postów: 10
Pomógł: 0
Dołączył: 29.08.2009

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


Dziękuje za pomoc. Skorzystałem z get_called_class ale pod php 5.2 (była w komentarzach zdefiniowana ta funkcja).
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: 20.08.2025 - 03:43