Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51240 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 2738 invoked from network); 7 Jan 2011 15:03:04 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Jan 2011 15:03:04 -0000 Authentication-Results: pb1.pair.com smtp.mail=martinscotta@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=martinscotta@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.170 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: martinscotta@gmail.com X-Host-Fingerprint: 209.85.216.170 mail-qy0-f170.google.com Received: from [209.85.216.170] ([209.85.216.170:64560] helo=mail-qy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 80/BA-25120-72B272D4 for ; Fri, 07 Jan 2011 10:03:03 -0500 Received: by qyk10 with SMTP id 10so522153qyk.8 for ; Fri, 07 Jan 2011 07:03:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:cc:content-type; bh=lHJtxgP461oVh3PrzlQWROq70Z61uvKw/f3A4KscpWE=; b=QzW1RFEh/I4Hh0yxOsOv34K5a5GYpxn+JaPo+JZvO36ZgiNKhRbfjCPY2cX88HJP3P dIOyWlqqTN5G+GTNe+XgF55x4XL1yf7LyWu6WCeKhEHWQCuhxXxzOWS2yFB9uVdBq2e+ h7yJb03Oz8iGVOB1NfEc3wt72Vbfc/uzYaZS4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=BExZvmzo0B5iwFUofueoxyZWl8JM8p7LoLT9p7CV8bWYkD2h+BeB0yygViENX3jsLE g1hMqEyX2PpEBQxGu4J3de+1TQduvIskagokmqXlc3CHNxWtGiGSLEJffPHc1jQrIB6B scVBrAQJLPOpUt4vl3f9YPR6Yo6WErevL28Y8= MIME-Version: 1.0 Received: by 10.224.11.71 with SMTP id s7mr24794027qas.13.1294412581273; Fri, 07 Jan 2011 07:03:01 -0800 (PST) Received: by 10.220.179.136 with HTTP; Fri, 7 Jan 2011 07:03:01 -0800 (PST) In-Reply-To: References: <1294276068.10675.3.camel@guybrush> Date: Fri, 7 Jan 2011 12:03:01 -0300 Message-ID: To: internals@lists.php.net Cc: Michael Morris Content-Type: multipart/alternative; boundary=0015175cb1de19826b049942ea8b Subject: Re: [PHP-DEV] RFC: about class names as values From: martinscotta@gmail.com (Martin Scotta) --0015175cb1de19826b049942ea8b Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi all, I've wrote a simple snippet to show the importance of this feature. This little piece of code, at a glance, tries to instantiate 2 same objects by different ways, but fails because of a silently code bug. -- start of code test(); $class =3D 'B\C\Foo'; $f2 =3D new $class; $f2->test(); } ?> --- end of code This works this way because class names in strings are treated as fully qualified class names. Although this script was created to reproduce an programmer error it helps to show the need for a safe way to pass class name as a values. As a side note, '\A\B\C\Foo' refers to the same class that 'A\B\C\Foo', but the string comparison will return false Regards, Martin Scotta On Thu, Jan 6, 2011 at 11:47 AM, Michael Morris wro= te: > +1 to this. In a similar vein (and similar reasons) consider ::PARENT as > well. Since parent is also a reserved word here too is a case where no co= de > in existence will be in conflict. So > > class Foo {} > class Bar extends Foo{} > > echo Bar::PARENT; // "Foo" > > 2011/1/6 Martin Scotta > > Yes, my intention was to only add a magic constant with the class, simila= r >> 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 MyCl= ass >> >> >> >> 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 firs= t >> >> 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 clas= s >> > 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 i= n >> 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 witho= ut >> 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 >> > >> > >> > > --0015175cb1de19826b049942ea8b--