two words about what php lacks
There are many occasions when I would want more features from php , I will list only two , that are related to my previous post , the one about Zend_Feed.
Late static bindings
One of the things I wanted for a time now , is the so called late static binding. I will let all the theoretical details and what is it good about to the many posts that will follow about this subject, I will just post an example of something that you can't do properly in php5. In my previous posts I had at some point a uml diagram , with two factories there. What I really wanted is to also provide something like:
abstract class Wu_Feed_Factory
{
private static $instance = null;
protected function __construct()
{
}
public static function factory()
{
$instance = $this->getInstance();
$feed = $instance->create();
$feed->add($this->buildSource());
}
abstract protected function buildSource();
//can be protected or public same thing
private function getInstance()
{
if($instance == null) {
//will always refer to Wu_Feed_Factory
//and this is the problem
$instance = new __CLASS__;
}
}
private function create()
{
$feed = new Wu_Feed();
//init code here or whatever
return $feed;
}
}
class CommentsFeed extends Wu_Feed_Factory
{
protected function getSource()
{
//.....
}
}
//later in the code
//woun't work , will always use the base class
//as an instance
$feed = CommentsFeed::factory();
If this code is necessary , it doesn't matter , I just wanted to provide an example. Here is a shorter one:
//extract from the official php documentation
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
self::who();
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test();//outputs A
Luckly , this feature will be released with php 5.3. and I will be able to play more with the static methods and attributes, cool ... but I needed it way sooner.
Method overloading
I saw many people arguing about this, but what php offers it is not method overloading, it's more like a method not found handler , which is very handy indeed .. but I am tired to do crap like this:
public function loadFromArray($array)
{
}
public function loadFromFile($path)
{
}
public function loadFromObject($object)
{
}
or faking it:
public function load($resource)
{
if(is_array($resource)) {
return $this->_loadFromArray($resource);
}
if(is_string($resource)) {
//etc
}
//etc
}
From my point of view , my interface would be much cleaner if there would be something like:
public function load(string $string);
public function load(array $values);
I believe this is much cleaner and I also believe that a class coded this way can be safely extended when new resources are added. You will not have to deal with countless methods or all kind of crap that emulates method overloading.
emphpower
Even if you may not agree with my points , chances are that you probably wanted something from php at some point and you were not the only one . What can you do when your voice in the php community is next to null(it's not impossible but it is a long journey)? It's difficult to make people listen to you if you are not a prolific blogger or you are not part of some cool open source projects. There is an alternative now , and it is called emphpower. It may not be the perfect solution , but it is the least you can do.
K.
Rss