Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:59936 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 13071 invoked from network); 14 Apr 2012 21:14:50 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Apr 2012 21:14:50 -0000 Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; 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:43695] helo=mail-gy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D5/B3-33137-7C8E98F4 for ; Sat, 14 Apr 2012 17:14:47 -0400 Received: by ghbg2 with SMTP id g2so2339980ghb.29 for ; Sat, 14 Apr 2012 14:14:44 -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=kHxA7PleYAJ8KfOkJL8tGCylzRRD3AgUQ8WiPnKy4rU=; b=CamNfR5g4onR5z8IgINNbfw6XRSS28LR6uVyt5fqGPE03P/XNzpMBo3krrP+tE0/DU Dz10hiZaTAF6/quLJYKeK7X9Z+UVkcj4wwUfr/2lsTCjEp8/yU7MN7yvtARpWpp1UL8+ W4YmU87bpVfVjUHbKNubmWANDEB5Z7eatV2zW545+whisHmBZS5D9rSd0cBGNgYjs/7r J1H9AZ9ja3BNVIW51IkriLZ7/uge+CFqpEELa3boxhefti2qcDMU8BR+vCicQ3IJsSRT hFjiTMkDODJkWT6WVUeuecvGse7sTowD/6SmybaMblIpB3QaATFPI/o/XCqwMWDM4+1H rNLw== Received: by 10.236.153.36 with SMTP id e24mr6056900yhk.67.1334438084828; Sat, 14 Apr 2012 14:14:44 -0700 (PDT) MIME-Version: 1.0 Received: by 10.101.32.8 with HTTP; Sat, 14 Apr 2012 14:14:24 -0700 (PDT) In-Reply-To: <4F89D4F1.8070009@ralphschindler.com> References: <4F89D4F1.8070009@ralphschindler.com> Date: Sat, 14 Apr 2012 23:14:24 +0200 Message-ID: To: Ralph Schindler Cc: internals Content-Type: multipart/alternative; boundary=20cf302d4990055f5e04bdaa141f Subject: Re: [PHP-DEV] New Feature: Fully qualified class name resolution as scalar with class keyword From: ocramius@gmail.com (Marco Pivetta) --20cf302d4990055f5e04bdaa141f Content-Type: text/plain; charset=UTF-8 I used to implement `public static function getClass() { return get_called_class(); }`, so I really like this one, makes it also easier for IDEs when refactoring code :) I was wondering about `class A { const CLASS = 'hello'; }` but that would cause an unexpected `T_CLASS`, so I guess there's no conflicts... Marco Pivetta http://twitter.com/Ocramius http://marco-pivetta.com On 14 April 2012 21:50, 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/** > 02210d51851a96d723fbedcfc64cde**9f9ae2b22a > > ... 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 > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --20cf302d4990055f5e04bdaa141f--