php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #39177 Need mechanism to identifed calling class in static methods
Submitted: 2006-10-17 17:13 UTC Modified: 2006-11-11 01:34 UTC
From: acarheden at gmail dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.1.6 OS: Windows 2K3 Server
Private report: No CVE-ID: None
 [2006-10-17 17:13 UTC] acarheden at gmail dot com
Description:
------------
This bug is a duplicate of all of these bugs, but I have a new argument. I've also filed it correctly as a feature request:
http://bugs.php.net/bug.php?id=30140
http://bugs.php.net/bug.php?id=30423
http://bugs.php.net/bug.php?id=30934
http://bugs.php.net/bug.php?id=30235

When you call a static method on a subclass, the superclass has no way of knowning what, if any, subclass it was called from.

Say you wanted to implement an Object-Relational Model like ActiveRecord (Ruby) in PHP (I do). You write an abstract ActiveRecord class with a static method find() that gets records from the database. Users write some subclass, the name of which coorisponds to a database table. 

In Ruby's ActiveRecord something like
  class User extends ActiveRecord {}
is my entire implementation of the User class. I call User::find() and it returns all the User records (As User objects) from the database. The same thing is impossible in PHP because ActiveRecord::find() doesn't know it was called as User::find().

I understand that C++ and Java don't work the way Ruby does. I also understand that it's not a simple change to make and that it may have performance implications. But Ruby is slower, doesn't work well on Windows, and doesn't have as much code available as PHP, so it would be specacular if PHP could provide such functionality as well. 

Also, shouldn't you simply be able to include some method like get_called_class() that walks the stack trace to find this info. That doesn't seem like it should be very difficult OR impact the performance of things that don't use the new functionality.

Oh, and one more thing. Pleeeeaaassseeee!

Reproduce code:
---------------
class test {

 static function whoami() {
   print "I am " . __CLASS__; // Maybe __CALLED_CLASS__
 }
}

class test1 extends test {
}

test::whoami -> I am test

Expected result:
----------------
I expect the results I get...

Actual result:
--------------
..but I'd LIKE a way to get 'test1'.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-11-11 01:34 UTC] tony2001@php.net
Duplicate of FR #34804.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 10:01:32 2024 UTC