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





--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to