note 123365 added to function.get-parent-class
| From: | yukal dot alexander at gmail dot com | Date: | Sat, 24 Nov 2018 19:28:46 +0000 |
| Subject: | note 123365 added to function.get-parent-class | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209765@lists.php.net to get a copy of this message | ||
An output of the entire inheritance chain using closures, recursion, and OOP
class ParentClass {
public static function getChain() {
$chain = null;
return $function = function($className='') use (& $chain, & $function) {
if (empty($className))
$className = static::class;
if (empty($chain))
$chain = $className;
$parent = get_parent_class($className);
if ($parent !== false) {
$chain .= " > {$parent}";
return $function($parent);
}
return $chain;
};
}
}
class Child extends ParentClass {}
class SubChild extends Child {}
class Sub2 extends SubChild {}
class Sub3 extends Sub2 {}
class Sub4 extends Sub3 {}
class Sub5 extends Sub4 {}
class Sub6 extends Sub5 {}
class Sub7 extends Sub6 {}
printf("%s\n", Sub7::getChain()());
$getChain = Sub7::getChain();
printf("%s\n", $getChain('Sub3'));
Output is:
Sub7 > Sub6 > Sub5 > Sub4 > Sub3 > Sub2 > SubChild > Child > ParentClass
Sub3 > Sub2 > SubChild > Child > ParentClass
----
Server IP: 208.43.231.9
Probable Submitter: 93.77.32.73
----
Manual Page -- http://php.net/manual/en/function.get-parent-class.php
Edit -- https://master.php.net/note/edit/123365
Del: integrated -- https://master.php.net/note/delete/123365/integrated
Del: useless -- https://master.php.net/note/delete/123365/useless
Del: bad code -- https://master.php.net/note/delete/123365/bad+code
Del: spam -- https://master.php.net/note/delete/123365/spam
Del: non-english -- https://master.php.net/note/delete/123365/non-english
Del: in docs -- https://master.php.net/note/delete/123365/in+docs
Del: other reasons-- https://master.php.net/note/delete/123365
Reject -- https://master.php.net/note/reject/123365
Search -- https://master.php.net/manage/user-notes.php