Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60067 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 62377 invoked from network); 17 Apr 2012 09:17:10 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Apr 2012 09:17:10 -0000 Authentication-Results: pb1.pair.com header.from=nikita.ppv@googlemail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@googlemail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain googlemail.com designates 209.85.217.170 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@googlemail.com X-Host-Fingerprint: 209.85.217.170 mail-lb0-f170.google.com Received: from [209.85.217.170] ([209.85.217.170:52579] helo=mail-lb0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5E/35-34429-4153D8F4 for ; Tue, 17 Apr 2012 05:17:09 -0400 Received: by lbbgf7 with SMTP id gf7so4682391lbb.29 for ; Tue, 17 Apr 2012 02:17:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=LwKp1cHRZa1jWTLekqrjnJ473xY3nVLIfLBaZDzCKt8=; b=Mz5z85nC7IO1ABQ1ugFsS4hOJYPrnjkul5nr7N70Ena4PVGErCn1n1bWUF9Mrdwsn7 nUIqYDURU43RCEuXkjaPpECvHxTSzfQ4ZjoD5g/T+VapeKjfH8I3vlSwfY/+luG2DvWa O8uF0qEiBtBruzLuOfK+vE0YuHgh0WXa2lnTiakHF8fN/2uor3wOac3HExzofPSkbWLw HJb8do6tv7eYMBboTYm6k+hRQiJsLUGHhaq8GyKWzucCtZI7/k6c+xVtfB+vRhPBKwbE vzH2i6UjhoedPYKxpAQTwwCnbq1r6ws4m+UmqUX1Ot9O0LfTw/CuXj1iovO3U24D9SLG 75bA== MIME-Version: 1.0 Received: by 10.152.103.134 with SMTP id fw6mr13290807lab.20.1334654225795; Tue, 17 Apr 2012 02:17:05 -0700 (PDT) Received: by 10.152.127.68 with HTTP; Tue, 17 Apr 2012 02:17:05 -0700 (PDT) In-Reply-To: <4F89D4F1.8070009@ralphschindler.com> References: <4F89D4F1.8070009@ralphschindler.com> Date: Tue, 17 Apr 2012 11:17:05 +0200 Message-ID: To: Ralph Schindler Cc: internals Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] New Feature: Fully qualified class name resolution as scalar with class keyword From: nikita.ppv@googlemail.com (Nikita Popov) On Sat, Apr 14, 2012 at 9:50 PM, Ralph Schindler wrote: > Hi all, > > There are many different use cases were in code we expect classes names a= s > arguments to functions as fully qualified names. =A0We do this in ZF a lo= t > with our Service Location and DI components, but also with our code > reflection API, etc. =A0A more interesting use case I would like to call = out > is with PHPUnit, for example in a test, you might find this: > > =A0$mock =3D $this->getMock('A\Namespaced\ClassName'); > > This becomes cumbersome when you are dealing with lots of strings about l= ots > of class names. =A0This is also an area where, currently, namespace > declaration and use statements offer no real support. > > The patch located here: > > https://github.com/ralphschindler/php-src/commit/02210d51851a96d723fbedcf= c64cde9f9ae2b22a > > ... implements the ability for a developer to leverage the file's namespa= ce > declaration and use statements to be able to produce a scalar (string) of > the class name that can be then used, for example, as an argument to a > function elsewhere. > > This overloads the "class" keyword, and by virtue of the existing usage o= f > "class" this feature is completely backwards compatible. =A0All existing = tests > pass. =A0For example, the above PHPUnit snipped would become: > > =A0use A\Namespaced\ClassName; > =A0$mock =3D $this->getMock(ClassName::class); > > Another example with reflection: > > =A0use SomeOther\FullyNamespaced\ClassElsewhere as CE; > =A0$r =3D new ReflectionClass(CE::class); > > More examples from the test file: > > =A0namespace Foo\Bar { > =A0 =A0class Baz {} > =A0 =A0var_dump(Moo::CLASS); // "Foo\Bar\Moo" > =A0} > > =A0namespace { > =A0 =A0use Bee\Bop as Moo, > =A0 =A0 =A0 =A0Foo\Bar\Baz; > > =A0 =A0var_dump(Baz::class); // "Foo\Bar\Baz" > =A0 =A0var_dump(Boo::class); // "Boo" > =A0 =A0var_dump(Moo::CLASS); // "Bee\Bop" > =A0 =A0var_dump(\Moo::Class); // "Moo" > > =A0 =A0$class =3D Baz::class; // assign class as scalar to var > =A0 =A0$x =3D new $class; > =A0 =A0var_dump($x); =A0object(Foo\Bar\Baz)#1 (0) {} > =A0} > > > What do you guys think? Hey Ralph! I like the proposal :) A quick note on the patch: As the class name is compile-time resolvable it should in my eyes also be available as a `static_scalar`, so that it can be used in initialization lists: public function doFoo($withClass =3D ABC::class) { new $withClass; // or whatever } To be available as both a `static_scalar` and a general `scalar` one should put the rule in the `common_scalar` section. What do you think? Nikita