Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:59973 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 80648 invoked from network); 15 Apr 2012 23:13:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Apr 2012 23:13:27 -0000 Authentication-Results: pb1.pair.com smtp.mail=simonsimcity@googlemail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=simonsimcity@googlemail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain googlemail.com designates 209.85.214.170 as permitted sender) X-PHP-List-Original-Sender: simonsimcity@googlemail.com X-Host-Fingerprint: 209.85.214.170 mail-ob0-f170.google.com Received: from [209.85.214.170] ([209.85.214.170:38512] helo=mail-ob0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CD/31-05733-6165B8F4 for ; Sun, 15 Apr 2012 19:13:26 -0400 Received: by obbta17 with SMTP id ta17so5910499obb.29 for ; Sun, 15 Apr 2012 16:13:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=z+j10EdXuCnR15+aj4K7rhvxeP/t5HYg0oSYO3ZQ06w=; b=RKIteyOZPJ67rCcPMYSX8h/FwJ7J40svLRpe07j1miNz88yjyHS+Qhgg8kbucE+KV+ w+GuSjCz6LkSPg7EfDrFGDYSXd2SJtRlzWXLtUX+LDiENg8Ld0ozMazoHH5KylTfEkX1 XvS6fOymkRZd43XSoHvwmm6QMtoPYPqWcK1da0MrR2ZNaOSjidHFBYIaBNXkjXZ6Wmj9 PCyO9zuf7ieJHS784pI3yZzFicdIHrqyHNgYmH+91CMARgpQPhBbgOOHJlpR5he5iVGJ 1Hk2yKP0AUIw6OPV/sLEH8LFjNVNfXSMTXCLBl+lVHtqQzVIJ1InlrsHSyTAzCMacVP7 BnxQ== MIME-Version: 1.0 Received: by 10.60.28.137 with SMTP id b9mr13127027oeh.57.1334531603593; Sun, 15 Apr 2012 16:13:23 -0700 (PDT) Received: by 10.60.98.74 with HTTP; Sun, 15 Apr 2012 16:13:23 -0700 (PDT) In-Reply-To: <4F89D4F1.8070009@ralphschindler.com> References: <4F89D4F1.8070009@ralphschindler.com> Date: Mon, 16 Apr 2012 01:13:23 +0200 Message-ID: To: Ralph Schindler Cc: internals Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] New Feature: Fully qualified class name resolution as scalar with class keyword From: simonsimcity@googlemail.com (Simon Schick) 2012/4/14 Ralph Schindler : > Hi all, > > There are many different use cases were in code we expect classes names a= s > arguments to functions as fully qualified names. =C2=A0We do this in ZF a= lot > with our Service Location and DI components, but also with our code > reflection API, etc. =C2=A0A more interesting use case I would like to ca= ll out > is with PHPUnit, for example in a test, you might find this: > > =C2=A0$mock =3D $this->getMock('A\Namespaced\ClassName'); > > This becomes cumbersome when you are dealing with lots of strings about l= ots > of class names. =C2=A0This 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/02210d51851a96d723fbedcf= c64cde9f9ae2b22a > > ... implements the ability for a developer to leverage the file's namespa= ce > 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 o= f > "class" this feature is completely backwards compatible. =C2=A0All existi= ng tests > pass. =C2=A0For example, the above PHPUnit snipped would become: > > =C2=A0use A\Namespaced\ClassName; > =C2=A0$mock =3D $this->getMock(ClassName::class); > > Another example with reflection: > > =C2=A0use SomeOther\FullyNamespaced\ClassElsewhere as CE; > =C2=A0$r =3D new ReflectionClass(CE::class); > > More examples from the test file: > > =C2=A0namespace Foo\Bar { > =C2=A0 =C2=A0class Baz {} > =C2=A0 =C2=A0var_dump(Moo::CLASS); // "Foo\Bar\Moo" > =C2=A0} > > =C2=A0namespace { > =C2=A0 =C2=A0use Bee\Bop as Moo, > =C2=A0 =C2=A0 =C2=A0 =C2=A0Foo\Bar\Baz; > > =C2=A0 =C2=A0var_dump(Baz::class); // "Foo\Bar\Baz" > =C2=A0 =C2=A0var_dump(Boo::class); // "Boo" > =C2=A0 =C2=A0var_dump(Moo::CLASS); // "Bee\Bop" > =C2=A0 =C2=A0var_dump(\Moo::Class); // "Moo" > > =C2=A0 =C2=A0$class =3D Baz::class; // assign class as scalar to var > =C2=A0 =C2=A0$x =3D new $class; > =C2=A0 =C2=A0var_dump($x); =C2=A0object(Foo\Bar\Baz)#1 (0) {} > =C2=A0} > > > What do you guys think? > > -ralph > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > Hi, Ralph I really like this feature in general. One thing I personally dislike in this implementation is the difference between CLASS and class ... One with and one without namespaces ... If we can unify that, this would be a great help in understanding that feature in future and getting the difference while reading code. And please don't forget: Code will be written once, maybe rewritten some times, but it will be read way more times! Bye Simon