php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #30423 get_class(self) for static methods
Submitted: 2004-10-13 20:18 UTC Modified: 2004-10-27 21:58 UTC
Votes:155
Avg. Score:4.7 ± 0.6
Reproduced:147 of 147 (100.0%)
Same Version:96 (65.3%)
Same OS:90 (61.2%)
From: auroraeosrose at hotmail dot com Assigned: helly (profile)
Status: Wont fix Package: Feature/Change Request
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
 [2004-10-13 20:18 UTC] auroraeosrose at hotmail dot com
Description:
------------
When using parent - child classes you sometimes need to get the name of the child, not the parent FROM the parent class.  In a regular object instance you can do get_class($this);  However in static methods, you cannot.  I'd really like a get_class(self); so that you can get the same functionality in a static method.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-13 23:09 UTC] helly@php.net
Simply use get_class()
 [2004-10-14 03:22 UTC] auroraeosrose at hotmail dot com
only problem is, get_class() returns what __CLASS__ returns, the actual class your in, which is nice unless you need to know the class CALLED not the PARENT

extremely simplistic view:

class test {
   function whoami() {
     echo "Hello, I'm whoami 1 !\r\n";
     echo "Value of __CLASS__ : ".__CLASS__."\r\n";
     echo "Value of get_class(\$this) : ".get_class($this)."\r\n\r\n";
     echo "Value of get_class() : ".get_class()."\r\n\r\n";
   }
  }
  class test2 extends test {
  }
  $test=new test;
  $test->whoami();
  $test2=new test2;
  $test2->whoami();

Now, you'll notice that get_class() is always returning the class whoami is in - test, whereas get_class($this) is returning the CHILD class, test2 - bug or feature? in any case it makes using get_class() as useless for me as __CLASS__ , I need to know the class called, not the class the function is in!
 [2004-10-14 03:23 UTC] auroraeosrose at hotmail dot com
oops, here are the results of my little test

Hello, I'm whoami 1 !
Value of __CLASS__ : test
Value of get_class($this) : test

Value of get_class() : test

Hello, I'm whoami 1 !
Value of __CLASS__ : test
Value of get_class($this) : test2

Value of get_class() : test
 [2004-10-14 09:27 UTC] helly@php.net
For PHP 5.0.* use get_parent_class(__CLASS__) or get_parent_class(get_class())

In HEAD i allowed to omit the classname in get_parent_class() now.
 [2004-10-14 14:58 UTC] auroraeosrose at hotmail dot com
strangely enough, in php 5.0.x I can't get get_parent_class(get_class()) or get_parent_class(__CLASS__) to work anywhere - and even though I threw on the lastest snap  of 5.1 get_parent_class() doesn't work yet, but it just might not be compiled in yet...I just get false back

What I'm looking for is for whoami1 to give me the get_class($this) answer from whoami - the actual class called

class test {
   function whoami() {
     echo 'This is class test'."\n";
     echo 'Value of __CLASS__ is '.__CLASS__."\n";
     echo 'Value of get_class() is '.get_class()."\n";
     echo 'Value of get_class($this) is '.get_class($this)."\n";
     echo 'Value of get_parent_class(__CLASS__) is '.get_parent_class(__CLASS__)."\n";
     echo 'Value of get_parent_class(get_class()) is '.get_parent_class(get_class())."\n\n";
   }
    static function whoami1() {
     echo 'This is class test'."\n";
     echo 'Value of __CLASS__ is '.__CLASS__."\n";
     echo 'Value of get_class() is '.get_class()."\n";
     echo 'Value of get_parent_class(__CLASS__) is '.get_parent_class(__CLASS__)."\n";
     echo 'Value of get_parent_class(get_class()) is '.get_parent_class(get_class())."\n\n";
   }
  }
  class test2 extends test {}
  $test=new test;
  $test->whoami();
  $test2=new test2;
  $test2->whoami();
  test::whoami1();
  test2::whoami1();

Results with php 5.0.*

This is class test
Value of __CLASS__ is test
Value of get_class() is test
Value of get_class($this) is test
Value of get_parent_class(__CLASS__) is 
Value of get_parent_class(get_class()) is 

This is class test
Value of __CLASS__ is test
Value of get_class() is test 
Value of get_class($this) is test2
 (note: -> this is what I want, only statically)
Value of get_parent_class(__CLASS__) is 
Value of get_parent_class(get_class()) is 

This is class test
Value of __CLASS__ is test
Value of get_class() is test
Value of get_parent_class(__CLASS__) is 
Value of get_parent_class(get_class()) is 

This is class test
Value of __CLASS__ is test
Value of get_class() is test 
Value of get_parent_class(__CLASS__) is 
Value of get_parent_class(get_class()) is

So I guess the long and short of it is it still doesn't work - I know I'm being a pain :)
 [2004-10-14 23:19 UTC] helly@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Just await 5.0.3, 5.1.0 or the next snaps build....
 [2004-10-15 20:18 UTC] auroraeosrose at hotmail dot com
I don't think you looked closely enough at the example code to understand what I was asking for.  Notice the method I'm calling is the parent, NOT the child.  I need the CHILD name not the PARENT.

I'm extending a class(an abstract one, not that it matters), but need to be able to get the name of the child class called in a method in the parent(abstract) class, because any number of classes could extend the abstract class.  

There's a trick to using get_class() to find out this information, if I call a method normally.  I can do get_class($this) inside the method.  HOWEVER, you can't DO that inside a static method, for obvious reasons there's no $this.  I need a way to do a get_class($this) equivalent in a static method!! - something like get_class(self).  Calling get_parent_class IN the parent class does me no good whatsoever- you just get false all the time.  I already KNOW the parent, I know how to get the parent, but I need the child!

(Actually, the ideal thing would be to fix get_class() so it returns the name of the actual class called, right now it returns whatever class the method happens to be in, and let __CLASS__ be the name of the class the method is actually in.)
 [2004-10-16 19:44 UTC] helly@php.net
If i get you right you want to know to which class the function call was up to? If that's the answer i can tell you that we won't fix it because it would require a new global executor variable.
 [2004-10-24 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2004-10-27 20:16 UTC] auroraeosrose at hotmail dot com
Then this is a won't fix...sigh...wanna set it to that?
 [2010-12-27 17:44 UTC] s at myriapod dot com
I think this was actually fixed in php 5.3 with the function get_called_class()
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 24 17:01:29 2024 UTC