note 107964 added to function.get-class
| From: | macnimble at gmail dot com | Date: | Sat, 17 Mar 2012 19:24:37 +0000 |
| Subject: | note 107964 added to function.get-class | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186722@lists.php.net to get a copy of this message | ||
Need a quick way to parse the name of a class when it's namespaced? Try this:
<?php
namespace Engine;
function parse_classname ($name)
{
return array(
'namespace' => array_slice(explode('\\', $name), 0, -1),
'classname' => join('', array_slice(explode('\\', $name), -1)),
);
}
final class Kernel
{
final public function __construct ()
{
echo '<pre>', print_r(parse_classname(__CLASS__),1), '</pre>';
// Or this for a one-line method to get just the classname:
// echo join('', array_slice(explode('\\', __CLASS__), -1));
}
}
new Kernel();
?>
Outputs:
Array
(
[namespace] => Array
(
[0] => Engine
)
[classname] => Kernel
)
----
Server IP: 208.69.120.55
Probable Submitter: 67.188.183.99
----
Manual Page -- http://www.php.net/manual/en/function.get-class.php
Edit -- https://master.php.net/note/edit/107964
Del: integrated -- https://master.php.net/note/delete/107964/integrated
Del: useless -- https://master.php.net/note/delete/107964/useless
Del: bad code -- https://master.php.net/note/delete/107964/bad+code
Del: spam -- https://master.php.net/note/delete/107964/spam
Del: non-english -- https://master.php.net/note/delete/107964/non-english
Del: in docs -- https://master.php.net/note/delete/107964/in+docs
Del: other reasons-- https://master.php.net/note/delete/107964
Reject -- https://master.php.net/note/reject/107964
Search -- https://master.php.net/manage/user-notes.php