note 84050 deleted from language.constants.predefined by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 11:01:41 +0000 |
| Subject: | note 84050 deleted from language.constants.predefined by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197031@lists.php.net to get a copy of this message | ||
Note Submitter: me at jamessocol dot com
----
We need an eighth magic constant, something along the lines of __STATIC__. This should return the
name of the class from which a static method was called, regardless of where in the inheritance tree
the method was defined.
PHP 5.3 has the new use of the static keyword which will help, but it isn't perfect. You still
have to repeat yourself frequently.
For example, trying to implement Active Record:
<?php
// In PHP 5.3
class Model
{
public static function find()
{
echo static::$class;
}
}
class Product extends Model
{
protected static $class = __CLASS__;
}
class User extends Model
{
protected static $class = __CLASS__;
}
Product::find(); // "Product"
User::find(); // "User"
?>
<?php
// With __STATIC__ keyword. (Would be better.)
class Model
{
public static function find()
{
echo __STATIC__;
}
}
class Product extends Model {}
class User extends Model {}
Product::find(); // "Product"
User::find(); // "User"
?>
[EDITED : Use get_called_class()]