Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> __CLASS__
UDAT
post 28.12.2005, 10:02:52
Post #1





Grupa: Zarejestrowani
Postów: 442
Pomógł: 0
Dołączył: 27.12.2005

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


Jak pobrać nazwę klasy z obiektu??
Niestety __CLASS__ nie działa, gdyż w momencie:
  1. <?php
  2.  
  3. class foo
  4. {
  5.    function getClass()
  6.    {
  7.          return __CLASS__;
  8.     }
  9.     // coś tam
  10. }
  11.  
  12. class foo2 extends foo
  13. {
  14. //coś tam
  15. }
  16.  
  17. ob=new foo2;
  18. echo ob->getClass(); 
  19.  
  20. ?>

wypisuje foo sad.gif a powinno foo2

Ten post edytował UDAT 28.12.2005, 10:58:08
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 13)
SongoQ
post 28.12.2005, 10:17:19
Post #2





Grupa: Przyjaciele php.pl
Postów: 2 923
Pomógł: 9
Dołączył: 25.10.2004
Skąd: Rzeszów - studia / Warszawa - praca

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


Jest ok. _CLASS_ Ci zwocil nazwe klasy w ktorej sie znajduje.


--------------------
Go to the top of the page
+Quote Post
UDAT
post 28.12.2005, 10:20:48
Post #3





Grupa: Zarejestrowani
Postów: 442
Pomógł: 0
Dołączył: 27.12.2005

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


Mi właśnie chodzi o to aby zwrócił FOO2 (klasę obiektu, a nie klasę z której obiekt dziedziczy)
Go to the top of the page
+Quote Post
ActivePlayer
post 28.12.2005, 10:23:33
Post #4





Grupa: Przyjaciele php.pl
Postów: 1 224
Pomógł: 40
Dołączył: 6.07.2004
Skąd: Wuppertal

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


a moze get_class" title="Zobacz w manualu php" target="_manual ?
czyli w Twoim przypadku
  1. <?php
  2.  
  3. class foo
  4. {
  5.  function getClass()
  6.  {
  7.  return get_class($this);
  8. }
  9. // coś tam
  10. }
  11.  
  12. ?>
Go to the top of the page
+Quote Post
UDAT
post 28.12.2005, 10:31:31
Post #5





Grupa: Zarejestrowani
Postów: 442
Pomógł: 0
Dołączył: 27.12.2005

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


  1. <?php
  2.  
  3. class foo
  4. {
  5.    function getClass()
  6.    {
  7.          return get_class($this);
  8.     }
  9.     // coś tam
  10. }
  11.  
  12. class foo2 extends foo
  13. {
  14. //coś tam
  15. }
  16.  
  17. echo foo2::getClass(); 
  18.  
  19. ?>


Get_class działa tylko, że funkcja getClass ma być statyczna, a wtedy niestety ale $this=NULL, więc nie mogę tego użyć

//Zły przykład podałem tongue.gif

Ten post edytował UDAT 28.12.2005, 10:34:07
Go to the top of the page
+Quote Post
ActivePlayer
post 28.12.2005, 10:34:48
Post #6





Grupa: Przyjaciele php.pl
Postów: 1 224
Pomógł: 40
Dołączył: 6.07.2004
Skąd: Wuppertal

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


omg to jak wykonujesz metode statyczna to przeciez piszesz:

nazwa_klasy::nazwa_funkcji()... wiec po co Ci nazwa klasy jesli ją posiadasz ?
Go to the top of the page
+Quote Post
dtb
post 28.12.2005, 10:35:59
Post #7





Grupa: Zarejestrowani
Postów: 476
Pomógł: 1
Dołączył: 5.11.2005
Skąd: Bieruń city

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


z tego co mi wiadomo typ obiektu to nazwa klasy:
spróbuj z gettype()


--------------------
Go to the top of the page
+Quote Post
ActivePlayer
post 28.12.2005, 10:37:35
Post #8





Grupa: Przyjaciele php.pl
Postów: 1 224
Pomógł: 40
Dołączył: 6.07.2004
Skąd: Wuppertal

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


nie zadziala

Kod
Possibles values for the returned string are:

    *

      "boolean" (since php 4)
    *

      "integer"
    *

      "double" (for historical reasons "double" is returned in case of a float, and not simply "float")
    *

      "string"
    *

      "array"
    *

      "object"
    *

      "resource" (since php 4)
    *

      "NULL" (since php 4)
    *

      "user function" (php 3 only, deprecated)
    *

      "unknown type"
Go to the top of the page
+Quote Post
UDAT
post 28.12.2005, 10:40:10
Post #9





Grupa: Zarejestrowani
Postów: 442
Pomógł: 0
Dołączył: 27.12.2005

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


Tak ale mi nie o to chodzi otóż chce mieć klasę (foo) która udostępnia funkcję, która pobiera nazwę klasy, do której należy obiekt, który ją dziedziczy
  1. <?php
  2.  
  3. class foo
  4. {
  5.    function make()
  6.    {
  7.          $klasa=get_class($this);
  8.  $klasa::makeSth;
  9.  $klasa::makeSth1;
  10.  $klasa::makeSth2;
  11.     }
  12.     // coś tam
  13. }
  14.  
  15. class foo2 extends foo
  16. {
  17. //coś tam
  18.  function makeSth()
  19. {
  20. }
  21.  function makeSth1()
  22. {
  23. }
  24.  function makeSth2()
  25. {
  26. }
  27.  
  28. }
  29.  
  30. $ob=new foo2();
  31. echo foo2->make(); 
  32.  
  33. ?>
Go to the top of the page
+Quote Post
ActivePlayer
post 28.12.2005, 10:44:52
Post #10





Grupa: Przyjaciele php.pl
Postów: 1 224
Pomógł: 40
Dołączył: 6.07.2004
Skąd: Wuppertal

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


  1. <?php
  2. class foo
  3. {
  4. function make(){
  5. $this->makeSth();
  6. $this->makeSth1();
  7. $this->makeSth2();
  8. }
  9. // coś tam
  10. function makeSth(){
  11. return false;
  12. }
  13.  
  14. function makeSth1(){
  15. return false;
  16. }
  17.  
  18. function makeSth2(){
  19. return false;
  20. }
  21.  
  22. }
  23.  
  24. class foo2 extends foo
  25. {
  26. //coś tam
  27. function makeSth(){
  28. echo 'makesth';
  29. }
  30.  
  31. function makeSth1(){
  32. echo 'makesth1';
  33. }
  34.  
  35. function makeSth2(){
  36. echo 'makesth2';
  37. }
  38.  
  39. }
  40.  
  41. $ob=new foo2();
  42. echo foo2->make(); 
  43. ?>

moze tak ?smile.gif
Go to the top of the page
+Quote Post
UDAT
post 28.12.2005, 10:53:46
Post #11





Grupa: Zarejestrowani
Postów: 442
Pomógł: 0
Dołączył: 27.12.2005

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


A nie da się inaczej questionmark.gifquestionmark.gifquestionmark.gif

Bo w momencie:
  1. <?php
  2.  
  3. class foo
  4. {
  5.    function make()
  6.    {
  7.          $klasa=get_class($this);
  8.                      $klasa::makeSth;                    
  9.                      $klasa::makeSth1;
  10.                      $klasa.='admin_';
  11.                    $admin=new $klasa();
  12.                       $admin->Sth;
  13.     }
  14.     // coś tam
  15. }
  16.  
  17. class foo2 extends foo
  18. {
  19. //coś tam
  20.        function makeSth()
  21.       {
  22.       }
  23.        function makeSth1()
  24.       {
  25.       }
  26.        function makeSth2()
  27.       {
  28.       }
  29.  
  30. }
  31.  
  32. $ob=new foo2();
  33. echo foo2->make(); 
  34.  
  35. ?>

nie zadziała tongue.gif

Ten post edytował UDAT 28.12.2005, 10:54:05
Go to the top of the page
+Quote Post
brachu
post 28.12.2005, 11:38:28
Post #12





Grupa: Zarejestrowani
Postów: 92
Pomógł: 0
Dołączył: 13.04.2005

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


nie czaje co Ci nie zadziala?questionmark.gif no jasne jak nie bedziesz mial klasy o takiej nazwie jak sobie stworzysz to nie zadziala!!!


--------------------
www.tanieprogramy.edu.pl
www.zamkor.pl
Go to the top of the page
+Quote Post
Vengeance
post 28.12.2005, 12:16:14
Post #13





Grupa: Zarejestrowani
Postów: 657
Pomógł: 2
Dołączył: 15.08.2003
Skąd: Łódź

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


a czy to zadziala:
  1. <?php
  2.  
  3. class foo
  4. {
  5.  function make()
  6.  {
  7.  self::makeSth1();
  8.  self::makeSth2();
  9. }
  10. // coś tam
  11. }
  12.  
  13. class foo2 extends foo
  14. {
  15. //coś tam
  16.  function makeSth()
  17. {
  18. }
  19.  function makeSth1()
  20. {
  21. }
  22.  function makeSth2()
  23. {
  24. }
  25.  
  26. }
  27.  
  28. $ob=new foo2();
  29. echo foo2->make(); 
  30.  
  31. ?>


questionmark.gif


--------------------
Go to the top of the page
+Quote Post
UDAT
post 28.12.2005, 12:16:31
Post #14





Grupa: Zarejestrowani
Postów: 442
Pomógł: 0
Dołączył: 27.12.2005

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


Dobra już nie trzeba. Działa.

PS. Miała to być klasa do dziedziczenia, z którą dziedziczysz Singletona biggrin.gif
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: 14.08.2025 - 08:34