Re: Request for Comments: Horizontal Reuse for PHP

From: Date: Wed, 12 Nov 2008 03:17:27 +0000
Subject: Re: Request for Comments: Horizontal Reuse for PHP
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-41862@lists.php.net to get a copy of this message
class Helloworld { public function sayHello() { print "HELLO"; } } category HelloworldExtras on Helloworld { public function sayWorld() { print "World"; } } $h = new Helloworld; $h->sayWorld(); // print "WORLD" This is extremely beneficial for lightweight "extension" of classes without subclassing.
This feature is identical to the C# .NET "Extension" methods. The best use of it is for selectively extending the primitive type classes on the fly with methods you need (string, int, array, etc.). The irony in this is, in PHP the primitive types are not OOP capable, so with this in mind I wonder how much this functionality would serve to make the life of people easier, and how much of it will be just the rope they need to hang themselves. The concept of categories and extensions is not as popular to the average developer as say classes and interfaces. Also we can't overload "use" for this as "use" specifically only imports symbol names, not actual code, into the environment. It doesn't have the knowledge of whether something is a namespace, or a class, or a category, so to know how to trigger autoload. Regards, Stan Vassilev

« previous php.internals (#41862) next »