Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51250 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 14196 invoked from network); 9 Jan 2011 14:08:09 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Jan 2011 14:08:09 -0000 Authentication-Results: pb1.pair.com header.from=martin@sitevision.dk; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=martin@sitevision.dk; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain sitevision.dk designates 77.233.245.157 as permitted sender) X-PHP-List-Original-Sender: martin@sitevision.dk X-Host-Fingerprint: 77.233.245.157 mail01.sitevision.dk Windows 2000 SP4, XP SP1 Received: from [77.233.245.157] ([77.233.245.157:3046] helo=mail02.sitevision.dk) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 91/2D-33563-441C92D4 for ; Sun, 09 Jan 2011 09:08:08 -0500 Received: from mail-qw0-f42.google.com [209.85.216.42] by mail02.sitevision.dk with ESMTP (SMTPD-11.01) id ed9e00015e910550; Sun, 9 Jan 2011 15:07:59 +0100 Received: by qwj8 with SMTP id 8so19126721qwj.29 for ; Sun, 09 Jan 2011 06:07:57 -0800 (PST) MIME-Version: 1.0 Received: by 10.229.128.94 with SMTP id j30mr23789139qcs.297.1294582077510; Sun, 09 Jan 2011 06:07:57 -0800 (PST) Received: by 10.229.42.10 with HTTP; Sun, 9 Jan 2011 06:07:57 -0800 (PST) In-Reply-To: <4D283A96.3090908@yahoo.com.au> References: <1294276068.10675.3.camel@guybrush> <4D283A96.3090908@yahoo.com.au> Date: Sun, 9 Jan 2011 15:07:57 +0100 Message-ID: To: Ben Schmidt Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=000e0ce0da0adcd4a304996a6071 Subject: Re: [PHP-DEV] RFC: about class names as values From: martin@sitevision.dk (Martin Vium) --000e0ce0da0adcd4a304996a6071 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable I think adding a magic constant or method for getting the class name would be usefull in many scenarios, when referencing a specific class (e.g. factories, configurations). It would also work well with namespaces and refactoring tools e.g.: $mock =3D $this->getMock('\\My\\Custom\\Namespace\\MyClass'); vs. use My\Custom\Namespace\MyClass; $mock =3D $this->getMock(MyClass::CLASS); On 8 January 2011 11:21, Ben Schmidt wrote: > I think doing something like this is a good idea for classes and > interfaces. > > Ben. > > > > > On 7/01/11 1:16 AM, Martin Scotta wrote: > >> Yes, my intention was to only add a magic constant with the class, simil= ar >> to this >> >> namespace Bar { >> class Foo { >> const KLASS =3D __CLASS__; >> } >> } >> >> namespace Buzz { >> use \Bar\Foo as BazFoo; >> >> class Bar extends BazFoo { >> const KLASS =3D __CLASS__; >> } >> >> $bar =3D new Bar; >> $baz =3D new BazFoo; >> >> var_dump( get_class($baz), BazFoo::KLASS); >> var_dump( get_class($bar), Bar::KLASS ); >> } >> >> This is 100% valid PHP 5.3.3 code, but that includes a lot of effort fro= m >> the developer. Someone miss to include the KLASS constant on a class and >> the >> result is undefined. >> >> If that PHP could add a magic constant --named CLASS or whatever you >> like-- >> to each class it will reduce the amount of class names hardcoded onto >> strings, probably to zero. >> >> The only issue that I found today is related to interfaces. I'm not sure >> if >> they should include this sort of magic constant, but I would rather >> include >> them just for consistency but, as I previously said, I'm not sure about >> this >> one. >> >> Martin Scotta >> >> >> 2011/1/5 John LeSueur >> >> >>> >>> 2011/1/5 Johannes Schl=FCter >>> >>> On Wed, 2011-01-05 at 21:53 -0300, Martin Scotta wrote: >>>> >>>>> $obj =3D newInstance( MyClass ); // notice. undefined constant MyClas= s >>>>> >>>> >>>> This describes the major change with your idea. >>>> >>>> What happens if a constant MyClass exists? >>>> >>>> Another question is something like this: >>>> >>>> >>> function factory($class) { >>>> return new $class(); >>>> } >>>> >>>> factory( SomeClass ); >>>> ?> >>>> >>>> >>>> To proper support this we'd have to make classes first class elements. >>>> For making this consistent it would make sense to make functions first >>>> class elements. And best drop the $ in front of variables and create a >>>> new language. Everything else becomes a mess. >>>> >>>> johannes >>>> >>>> >>>> >>>> -- >>>> PHP Internals - PHP Runtime Development Mailing List >>>> To unsubscribe, visit: http://www.php.net/unsub.php >>>> >>>> >>>> I think he's actually proposing creating for each class the magic cla= ss >>> constant CLASS, so your example becomes: >>> >>> >>> >> >>> function factory($class) { >>> return new $class(); >>> } >>> >>> factory( SomeClass::CLASS ); >>> >>> ?> >>> >>> This is actually doable without a magic class constant, but requires a >>> function or class constant to be declared in each class. >>> >>> >> class SomeClass { >>> const CLASS =3D __NAMESPACE__ . '\' . __CLASS__; >>> static function getNameWithNSPath() >>> { >>> return __NAMESPACE__ . '\' . __CLASS__; >>> } >>> } >>> >>> >>> factory( SomeClass::getNameWithNSPath() ); >>> ?> >>> >>> Perhaps this could be simplified with traits, if __NAMESPACE__ and >>> __CLASS__ work in traits that way. In fact, that's an interesting >>> question, >>> what is __NAMESPACE__ in a trait defined in one namespace, then used in= a >>> class in a different namespace? >>> >>> I think the point is that the factory function could exist without any >>> knowledge of the namespaces of the classes it would work on. Then, >>> somewhere >>> else where the class has been aliased or is otherwise accessible withou= t >>> the >>> full namespace path, the developer wouldn't need to specify the full >>> namespace path to the factory, but could ask the class itself what it's >>> full >>> namespace path was. I don't know that I agree with the idea, but I don'= t >>> think it requires making classes first class elements. >>> >>> John >>> >>> >>> >> > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > > --=20 Mvh Martin Vium Senior System Arkitekt Sitevision --000e0ce0da0adcd4a304996a6071--