note 84050 added to language.constants.predefined
| From: | me at jamessocol dot com | Date: | Wed, 25 Jun 2008 16:11:00 +0000 |
| Subject: | note 84050 added to language.constants.predefined | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140812@lists.php.net to get a copy of this message | ||
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"
?>
----
Server IP: 69.147.83.197
Probable Submitter: 35.10.41.49
----
Manual Page -- http://www.php.net/manual/en/language.constants.predefined.php
Edit -- https://master.php.net/note/edit/84050
Del: integrated -- https://master.php.net/note/delete/84050/integrated
Del: useless -- https://master.php.net/note/delete/84050/useless
Del: bad code -- https://master.php.net/note/delete/84050/bad+code
Del: spam -- https://master.php.net/note/delete/84050/spam
Del: non-english -- https://master.php.net/note/delete/84050/non-english
Del: in docs -- https://master.php.net/note/delete/84050/in+docs
Del: other reasons-- https://master.php.net/note/delete/84050
Reject -- https://master.php.net/note/reject/84050
Search -- https://master.php.net/manage/user-notes.php