Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:91005 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 74816 invoked from network); 28 Jan 2016 23:26:04 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Jan 2016 23:26:04 -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.54 as permitted sender) X-PHP-List-Original-Sender: fontanalorenz@gmail.com X-Host-Fingerprint: 74.125.82.54 mail-wm0-f54.google.com Received: from [74.125.82.54] ([74.125.82.54:33071] helo=mail-wm0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 70/B2-53831-A83AAA65 for ; Thu, 28 Jan 2016 18:26:04 -0500 Received: by mail-wm0-f54.google.com with SMTP id l66so45990172wml.0 for ; Thu, 28 Jan 2016 15:26:02 -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=oPzEr+OdM2fGbpI7h9pbKShAbCepVZqqnLp9P8ZgS8E=; b=JrFPexMsaKD3q5OvzrZZj4pgjdJkXfzHVrI7lEQKR28XQGoaACP/7GHMxr+wKJRdU6 t1AKhNsPSroDrSLHDjgPLzXhsbqHR9Qv3vXToNnbrfmskueW/7SfysXLuX21jKVtC4cx 6b8uoOpy3fCoDD5N7HXK4uy+LMT9VV0486MIKB7nsoDhBl+2RnwqcjNSYWmq90R941Oa 5ZMXafOTPFWIR0E/USHXHneuj71z3XJXxAMAHZ/+uHsGPgTDSZxph1tBWeTjfTkWabM7 q3AJcr0i3XUa72M2PS9BCv4SuBpMoerZovGJ1sZEhanIzbOYxFiUb+S+cSwYW/zuRWM+ MJ8w== 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=oPzEr+OdM2fGbpI7h9pbKShAbCepVZqqnLp9P8ZgS8E=; b=Eki9sFbjnyeC5qdA4WgP+JhlFh8jodD0IB8tM2NyBl+tc2M7bMLuw0idB7bYhI0Kub xCp7S1FVjQkEf94Vtg8yQ5GZ8dLLIuygY08F0FPDNP2KKJCXDLYaIwQQ4c282L+2c51z LAzRoGVDu31lMJKxh9xHvsEdcD8dD2gyU3DUzofEQwcuELLxHtA/P/0BYfrSwqYldd5e 7KON+3e81A/v549eYtW6zwovyUYfpo0DaQ7sFcHruQi9IG7eEgodxBlTTLE6+Wqsh8Y9 yl5L5Piqrp4acx046a4MkOBVMcx4ImWW/jkwwpfLDKL6jrvSejRfoJQY4A7xigB3Bf3M FLdA== X-Gm-Message-State: AG10YOS1le54484ErwiyZ6+3nPNx83CacoXpcD4NLj2ZT6Rd1HInhcfssz6XODbavEI5rKyzbzoNCtIOVs3vaQ== MIME-Version: 1.0 X-Received: by 10.28.133.213 with SMTP id h204mr5851285wmd.5.1454023560477; Thu, 28 Jan 2016 15:26:00 -0800 (PST) Received: by 10.28.183.132 with HTTP; Thu, 28 Jan 2016 15:26:00 -0800 (PST) In-Reply-To: References: Date: Fri, 29 Jan 2016 00:26:00 +0100 Message-ID: To: Dan Ackroyd Cc: PHP internals Content-Type: multipart/alternative; boundary=001a1144336ad147ab052a6d3d1c Subject: Re: [PHP-DEV] get_class behavior From: fontanalorenz@gmail.com (Lorenzo Fontana) --001a1144336ad147ab052a6d3d1c Content-Type: text/plain; charset=UTF-8 2016-01-29 0:22 GMT+01:00 Lorenzo Fontana : > 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 > > > Another way to obtain the declaring class in static methods but it's not a callable is the use of *self::class* *class foo { static function bar() { echo self::class."\n"; echo get_called_class()."\n"; }}class Quux extends foo { }Quux::bar();// Output is:// foo// Quux* --001a1144336ad147ab052a6d3d1c--