Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:59931 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 275 invoked from network); 14 Apr 2012 19:50:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Apr 2012 19:50:15 -0000 Authentication-Results: pb1.pair.com smtp.mail=ralph@ralphschindler.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=ralph@ralphschindler.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain ralphschindler.com from 209.85.214.170 cause and error) X-PHP-List-Original-Sender: ralph@ralphschindler.com X-Host-Fingerprint: 209.85.214.170 mail-ob0-f170.google.com Received: from [209.85.214.170] ([209.85.214.170:39223] helo=mail-ob0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C5/31-33137-6F4D98F4 for ; Sat, 14 Apr 2012 15:50:14 -0400 Received: by obbta17 with SMTP id ta17so4963074obb.29 for ; Sat, 14 Apr 2012 12:50:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding:x-gm-message-state; bh=GU3sOmyGiE3hphcn7vQ0wAymKW3slD+Avmyf3TYaIrw=; b=cNYC3rLRWDW6eMhumOeXBlRx0gB6GqCN+p1DV5zJVt5s0ESSZ+qp5nGbPP4JrziiD2 /YzrAslHQ9yqumA4BpwiSX9Y1NDSwESknr/nSBcHFRF9TwNatU0dwWv1nj0MH/WlfTlV jn4GqJ5D+mWfggqnx1VsO79/bXtpH9EML4GCeMcOEkB125WNf4h1UxeR9GEAHor4+mri kQTNXcpO81DIrW3Wthb9C9xFVdnkmQLye/ikEJ3xIf7gruzqGOjpiUW+rITGlpbL/nTx MLZNlt9Ie/Jv0DqUUN2zpjGlH+6ktDt89d8eL9bzNZ5DnMXNc35GVaowFvSwJRvWGFIF 4ltA== Received: by 10.60.2.6 with SMTP id 6mr8413512oeq.21.1334433011537; Sat, 14 Apr 2012 12:50:11 -0700 (PDT) Received: from ralph-macbook.local (ip174-73-14-247.no.no.cox.net. [174.73.14.247]) by mx.google.com with ESMTPS id s8sm11563553oec.1.2012.04.14.12.50.10 (version=SSLv3 cipher=OTHER); Sat, 14 Apr 2012 12:50:11 -0700 (PDT) Message-ID: <4F89D4F1.8070009@ralphschindler.com> Date: Sat, 14 Apr 2012 14:50:09 -0500 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.9) Gecko/20061207 Thunderbird/1.5.0.9 Mnenhy/0.7.4.666 MIME-Version: 1.0 To: internals Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQl0wfUgF1dRFZRTRedZ+uW+AmmgvknWNtegtftKTsnbsfb0FnzK0OHIliadk+fXlJE6/y1O Subject: New Feature: Fully qualified class name resolution as scalar with class keyword From: ralph@ralphschindler.com (Ralph Schindler) 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