note 103027 added to language.oop5.late-static-bindings
| From: | brendel at krumedia dot de | Date: | Mon, 21 Mar 2011 14:37:25 +0000 |
| Subject: | note 103027 added to language.oop5.late-static-bindings | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178052@lists.php.net to get a copy of this message | ||
In my opinion late static binding is an anti pattern but there are some benefits though:
<?php
class MyClass extends ClassReference
{
}
?>
MyClass::getQualifiedName() returns the full class name including namespace information. This
prevents you from passing class names as plain strings and still lets you take advantage of code
completion and type browsing.
MyClass::cast() ensures the type of an object to be of a certain class. This is a kind of type
hinting.
<?php
abstract class ClassReference
{
final public static function getQualifiedName()
{
return get_called_class();
}
final public static function cast($object)
{
$className = get_called_class();
if (!is_object($var) || !($object instanceof $className))
{
throw new \UnexpectedValueException("Cast failed. Argument is not of type " .
$className . ".");
}
return $object;
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 109.192.56.140
----
Manual Page -- http://www.php.net/manual/en/language.oop5.late-static-bindings.php
Edit -- https://master.php.net/note/edit/103027
Del: integrated -- https://master.php.net/note/delete/103027/integrated
Del: useless -- https://master.php.net/note/delete/103027/useless
Del: bad code -- https://master.php.net/note/delete/103027/bad+code
Del: spam -- https://master.php.net/note/delete/103027/spam
Del: non-english -- https://master.php.net/note/delete/103027/non-english
Del: in docs -- https://master.php.net/note/delete/103027/in+docs
Del: other reasons-- https://master.php.net/note/delete/103027
Reject -- https://master.php.net/note/reject/103027
Search -- https://master.php.net/manage/user-notes.php