Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61199 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64184 invoked from network); 13 Jul 2012 13:55:56 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Jul 2012 13:55:56 -0000 X-Host-Fingerprint: 208.107.183.205 host-205-183-107-208.midco.net Date: Fri, 13 Jul 2012 09:55:55 -0400 Received: from [208.107.183.205] ([208.107.183.205:24813] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5A/A6-17670-BE820005 for ; Fri, 13 Jul 2012 09:55:55 -0400 Message-ID: <5A.A6.17670.BE820005@pb1.pair.com> To: internals@lists.php.net References: <4F89D4F1.8070009@ralphschindler.com> <6B.C4.17670.11910005@pb1.pair.com> User-Agent: slrn/pre1.0.0-18 (Linux) X-Posted-By: 208.107.183.205 Subject: Re: New Feature: Fully qualified class name resolution as scalar with class keyword From: weierophinney@php.net (Matthew Weier O'Phinney) On 2012-07-13, "Eugene Leonovich" wrote: > I'm a bit confused by the "class" keyword in the syntax ClassName::class. > We already have the magic constant __CLASS__ which does exactly the same > class name resolving, if you refer it within the class. > > So why to introduce a new keyword instead of using __CLASS__, like > ClassName::__CLASS__? "class" is already a keyword, making it a simpler, easier to remember, and easier to type choice. > "Ralph Schindler" wrote in message > news:4F89D4F1.8070009@ralphschindler.com... >> 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? >> >> -ralph > > -- Matthew Weier O'Phinney Project Lead | matthew@zend.com Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc