Re: get_class behavior

From: Date: Thu, 28 Jan 2016 22:35:54 +0000
Subject: Re: get_class behavior
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-91002@lists.php.net to get a copy of this message
On 28 January 2016 at 19:17, Lorenzo Fontana <fontanalorenz@gmail.com> wrote: > Hello everybody! Hello Lorenzo, > If *$result* is *null* the output of *get_class* is the name of the > containing class instead of the name of the object I wanted. Yes, that is bad. Perhaps a smaller change would give you the desired behaviour though? If we just give a warning/error if any non-object type is passed as the parameter, that would achieve what you want wouldn't it? And it provides a slightly easier 'upgrade' path for people who, for whatever reason, are currently passing null to it. > the name of the containing class can be easily obtained via *__CLASS__ > or via get_class($this)* or via *get_called_class() The first two can't be used directly as a callable, and so can't be passed around as a function. And get_called_class() returns the late static binding class. So if the smaller change of just giving a warning/error on any non-object as a parameter gives the desired result, I would recommend doing just that. It might even be possible to do it as a bug fix... cheers Dan ## Refactoring if non-objects are no longer allowed. Current code: echo get_class($result); Would need to be changed to: if ($result === null) { echo get_class(); } else { echo get_class($result); } ## get_called_class is late static binding. class foo { static function bar() { //return $fn(null); echo get_class()."\n"; echo get_called_class()."\n"; } } class Quux extends foo { } Quux::bar(); // Output is: // foo // Quux ## Callables can passed around class foo { public static function bar(callable $fn) { return $fn(); } } echo foo::bar('get_class');

« previous php.internals (#91002) next »