2016-01-28 23:35 GMT+01:00 Dan Ackroyd <dan...@basereality.com>: > On 28 January 2016 at 19:17, Lorenzo Fontana <fontanalor...@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'); >
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. Yes is what I would like to do, start saying people that they should change that by triggering a deprecation notice so they can refactor their code. 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. I don't see any way to get the declaring class passing a callable as you pointed out in the following code if not using *get_class* without parameters. Any idea? > > class foo { > public static function bar(callable $fn) { > return $fn(); > } > } > echo foo::bar('get_class'); Thanks, Lorenzo