Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:91004 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 73345 invoked from network); 28 Jan 2016 23:22:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Jan 2016 23:22:41 -0000 Authentication-Results: pb1.pair.com header.from=fontanalorenz@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=fontanalorenz@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.47 as permitted sender) X-PHP-List-Original-Sender: fontanalorenz@gmail.com X-Host-Fingerprint: 74.125.82.47 mail-wm0-f47.google.com Received: from [74.125.82.47] ([74.125.82.47:38526] helo=mail-wm0-f47.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 89/52-53831-0C2AAA65 for ; Thu, 28 Jan 2016 18:22:40 -0500 Received: by mail-wm0-f47.google.com with SMTP id p63so45997827wmp.1 for ; Thu, 28 Jan 2016 15:22:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=w3748a/wrNGWUqnwbHPFoPGdG8w6E+quRvxEdm08IAU=; b=uo/LYONUwCm0KHN1wKDzIBlr9p3VI0Zoa8/ciG6QGLPqvBWfy1xIQQYVXsStub/7ek LbAZxGSCiBeTL2+baSAsDuGLs/gs9l5VsL9UCDnkATs38g+OW4DLQxvevgeVx+PyIGPI WkCP6WfHS2RKvEnPYSIVQhSqQwbD2Toi3ncFjjnUZEueNCl/oZIyKIhUdm8WRKKudyVc WyxkGJJ5emVsp0DclWCdbpHLxRoed82gPc0O81UHZLMITGbdQQcWa+jKm2qaI509eFZ2 Qqc2hfjxq1y7SIHiBmGF5CVUAxLETaI3TODtXJsE9InJXxIok9AsgNYmm563IvTlcERg j0zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=w3748a/wrNGWUqnwbHPFoPGdG8w6E+quRvxEdm08IAU=; b=g2JNC+OVxSkfpiaU+Tr+OL31vz6ICUZ8+/CLtQi3g2u1bSHv8s3e0XZzbz30e2q+gP HqKouIxTTVn6mg8TnCkXF8McGvXk8NAnrDcuPXMpwJr6fBx+yAVkvUPp/oDrAtBm8WAj XXcFtMqjDXr9IzJIifhNIR+auy9Q3iIk4soXL1qaVWWIyPCMYmInIXM4M+sV2pzr39yM z1UF3qvaXEaM+NKEK6CFD9+MEUTJEKvkUsRuRXxUeCtvspLrW/HsLPsFvWuReZBk8HXh VvU4YDE/S83qZcQFz0lGVHOLW3vpgPo6QvW28Z/FBVXGqaL2WctO0P4oFUAsds8jM3c2 7aHw== X-Gm-Message-State: AG10YOT+o4vzZiJLsdwNIA7y9BsgcNti6AdaIVYBzlMDRjzv4GkajwYtOFenqXSC0iD+4po4oVgFB/al1Rg1yA== MIME-Version: 1.0 X-Received: by 10.28.10.7 with SMTP id 7mr5457884wmk.57.1454023357890; Thu, 28 Jan 2016 15:22:37 -0800 (PST) Received: by 10.28.183.132 with HTTP; Thu, 28 Jan 2016 15:22:37 -0800 (PST) In-Reply-To: References: Date: Fri, 29 Jan 2016 00:22:37 +0100 Message-ID: To: Dan Ackroyd Cc: PHP internals Content-Type: multipart/alternative; boundary=001a114426b8be0902052a6d31e5 Subject: Re: [PHP-DEV] get_class behavior From: fontanalorenz@gmail.com (Lorenzo Fontana) --001a114426b8be0902052a6d31e5 Content-Type: text/plain; charset=UTF-8 2016-01-28 23:35 GMT+01:00 Dan Ackroyd : > On 28 January 2016 at 19:17, Lorenzo Fontana > 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 --001a114426b8be0902052a6d31e5--