Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51233 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 84768 invoked from network); 6 Jan 2011 14:17:02 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Jan 2011 14:17:02 -0000 Authentication-Results: pb1.pair.com header.from=martinscotta@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=martinscotta@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.42 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.42 mail-qw0-f42.google.com Received: from [209.85.216.42] ([209.85.216.42:36290] helo=mail-qw0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A9/41-10160-DDEC52D4 for ; Thu, 06 Jan 2011 09:17:02 -0500 Received: by qwj8 with SMTP id 8so16816418qwj.29 for ; Thu, 06 Jan 2011 06:16:59 -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=lf18frM8SQQ9LzO3h8rH10Wr5YFu0THMUwTQgvlUyUc=; b=HC4inQSkbI5ghzwVXkfgWvCuvUA7sW0XVnSsYzRUZKEIjE8rZINms09l4OIIOPasx3 fayqaNCnS/CA17Rm9a+iw0Q3eSmq9WfWIVKhDyafDz7S+zjugKuW8p4AnZfUlEj/2wQV AEUXLBB8az2rNhhbS9gxG+NaYEb+VAic3i5zo= 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=m9BIQTPg4S5SNSO8//giJv8gju0bXXOPDJNqAIsIl/6kv5l6PDbGVA7xYzIYYBk5N3 L/cZpZfBG2NXoVS4/DfhU6om5gmzav1uwD1CjMk+r5S3Lg9EzCbDDY18ujUY6Ih6TfVD QPXfpbAY5Q8YwPuXXVRsJA6qDw2v4gj98Ob44= MIME-Version: 1.0 Received: by 10.229.110.10 with SMTP id l10mr21263628qcp.56.1294323418902; Thu, 06 Jan 2011 06:16:58 -0800 (PST) Received: by 10.220.179.136 with HTTP; Thu, 6 Jan 2011 06:16:58 -0800 (PST) In-Reply-To: References: <1294276068.10675.3.camel@guybrush> Date: Thu, 6 Jan 2011 11:16:58 -0300 Message-ID: To: internals@lists.php.net Cc: John LeSueur , =?ISO-8859-1?Q?Johannes_Schl=FCter?= Content-Type: multipart/alternative; boundary=0016368321b29bb3f504992e2717 Subject: Re: [PHP-DEV] RFC: about class names as values From: martinscotta@gmail.com (Martin Scotta) --0016368321b29bb3f504992e2717 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Yes, my intention was to only add a magic constant with the class, similar 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 from the developer. Someone miss to include the KLASS constant on a class and th= e 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 thi= s 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 MyClass >> >> 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 class > 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 questio= n, > 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, somewh= ere > else where the class has been aliased or is otherwise accessible without = 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 f= ull > 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 > > --0016368321b29bb3f504992e2717--