Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60068 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64102 invoked from network); 17 Apr 2012 09:22:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Apr 2012 09:22:43 -0000 Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.160.170 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 209.85.160.170 mail-gy0-f170.google.com Received: from [209.85.160.170] ([209.85.160.170:59193] helo=mail-gy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 19/A5-34429-1663D8F4 for ; Tue, 17 Apr 2012 05:22:41 -0400 Received: by ghbg2 with SMTP id g2so3280771ghb.29 for ; Tue, 17 Apr 2012 02:22:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=TLfoIbMzomZxaC2VLLHa6rm88MjQ0eQksc6v8B6K/4Q=; b=0qWQjWSOdmngaVpNIROMVACYNQHUaPNZ6b1F27IqbCYkn1/4EPVNhQucF7xRSsZrA0 Etx0AAr/vEly+TSe3rl+IrAzDyfrPgSym4ZysZESewugJImfVxyAdInSccY3DGAJ7vka c2KcojOr/uhRa//K8QCGNmHlt6FSvGyboCl/RWQ1wujstYLuhtjV4KDqCI/oleVz6VYD FV36D5afqxlxb0H29TwLPjCBcuIthDtFMKPOH81k13GGCH5fAS4JOfirsDD+ZK+l+Bxo by2rnxZ+F3tPCaDu3x+YnCI9Ye/3SmytBm2CJ1rItqsFGN/M5H4hO3ZW5yQ9YLDneZfa rIDA== Received: by 10.236.145.104 with SMTP id o68mr14458397yhj.74.1334654557851; Tue, 17 Apr 2012 02:22:37 -0700 (PDT) MIME-Version: 1.0 Received: by 10.101.32.8 with HTTP; Tue, 17 Apr 2012 02:22:17 -0700 (PDT) In-Reply-To: References: <4F89D4F1.8070009@ralphschindler.com> Date: Tue, 17 Apr 2012 11:22:17 +0200 Message-ID: To: Nikita Popov Cc: Ralph Schindler , internals Content-Type: multipart/alternative; boundary=20cf303b3cd9d18f5104bddc7adb Subject: Re: [PHP-DEV] New Feature: Fully qualified class name resolution as scalar with class keyword From: ocramius@gmail.com (Marco Pivetta) --20cf303b3cd9d18f5104bddc7adb Content-Type: text/plain; charset=UTF-8 @Nicolas: wouldn't __CLASS__ introduce a bit of confusion with the existing constant? Marco Pivetta http://twitter.com/Ocramius http://marco-pivetta.com On 17 April 2012 11:17, Nikita Popov wrote: > 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 > as > > arguments to functions as fully qualified names. We do this in ZF a lot > > with our Service Location and DI components, but also with our code > > reflection API, etc. A more interesting use case I would like to call > out > > is with PHPUnit, for example in a test, you might find this: > > > > $mock = $this->getMock('A\Namespaced\ClassName'); > > > > This becomes cumbersome when you are dealing with lots of strings about > lots > > of class names. This 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/02210d51851a96d723fbedcfc64cde9f9ae2b22a > > > > ... implements the ability for a developer to leverage the file's > namespace > > 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 > of > > "class" this feature is completely backwards compatible. All existing > tests > > pass. For example, the above PHPUnit snipped would become: > > > > use A\Namespaced\ClassName; > > $mock = $this->getMock(ClassName::class); > > > > Another example with reflection: > > > > use SomeOther\FullyNamespaced\ClassElsewhere as CE; > > $r = new ReflectionClass(CE::class); > > > > More examples from the test file: > > > > namespace Foo\Bar { > > class Baz {} > > var_dump(Moo::CLASS); // "Foo\Bar\Moo" > > } > > > > namespace { > > use Bee\Bop as Moo, > > Foo\Bar\Baz; > > > > var_dump(Baz::class); // "Foo\Bar\Baz" > > var_dump(Boo::class); // "Boo" > > var_dump(Moo::CLASS); // "Bee\Bop" > > var_dump(\Moo::Class); // "Moo" > > > > $class = Baz::class; // assign class as scalar to var > > $x = new $class; > > var_dump($x); object(Foo\Bar\Baz)#1 (0) {} > > } > > > > > > 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 = 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 > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --20cf303b3cd9d18f5104bddc7adb--