Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60054 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25969 invoked from network); 17 Apr 2012 04:02:09 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Apr 2012 04:02:09 -0000 Authentication-Results: pb1.pair.com header.from=ralph@ralphschindler.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=ralph@ralphschindler.com; spf=permerror; 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:47300] helo=mail-ob0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 55/2D-05733-04BEC8F4 for ; Tue, 17 Apr 2012 00:02:08 -0400 Received: by obbup16 with SMTP id up16so1024176obb.29 for ; Mon, 16 Apr 2012 21:02:05 -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:references :in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=kAAmpVO4hl6i9thuWiFbJKGIyCKZl+H6dd1xuE4Lr0k=; b=BckVhpPSOZAYUHoov9UAZaF28OPKSo3NoBv2E1BL5ewoPqMavaEqFrPhgxlQvF3a+u G5Zgaqzd0EgGNm5f6QMZUy0z0DQkdnCzjywOmHNFBKTjzA5tfyFbSNSeNcsgNDulziLQ 4H8IzJ/lqv4QsIW2UDnUbR41+UZrlJzbG+alf2YR2/Lu/Hf35VkIj12ABFqanxnm/Biy bYNbLe36VJJrJ0pGawyTEOtiRAgpDMiM9bbest3ZHMlTCrJJu8sqGx5r4RJEI4TpeRsg OS/7Nw19lytcq/92C3aagp4Y4vJxZjIKl6Zc2N8dz4xECPJlAdzp7NzieOSto0phG2Zl u6jA== Received: by 10.182.188.38 with SMTP id fx6mr18904000obc.77.1334635325508; Mon, 16 Apr 2012 21:02:05 -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 j10sm21924563oba.4.2012.04.16.21.02.03 (version=SSLv3 cipher=OTHER); Mon, 16 Apr 2012 21:02:04 -0700 (PDT) Message-ID: <4F8CEB3B.4080702@ralphschindler.com> Date: Mon, 16 Apr 2012 23:02:03 -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 References: <4F89D4F1.8070009@ralphschindler.com> In-Reply-To: <4F89D4F1.8070009@ralphschindler.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQnTWbbYd9qfZIzjL8skFZEbX393Vwkp1QLJtGl3U3eVcI14GT9dvsEBpCs1hQguNj42BG9v Subject: Re: New Feature: Fully qualified class name resolution as scalar with class keyword From: ralph@ralphschindler.com (Ralph Schindler) So, at current, is this small enough for just a pull request, or does this deserve its own RFC? -ralph On 4/14/12 2: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? > > -ralph