2

The dilemma I'm facing is: should I use static classes for the components of an application just to get nicer looking API?

Example - the "normal" way:

// example component
class Cache{
  abstract function get($k);
  abstract function set($k, $v);
}

class APCCache extends Cache{
  ...
}    

class application{

  function __construct()
    $this->cache = new APCCache();
  }

  function whatever(){
    $this->cache->add('blabla');
    print $this->cache->get('blablabla');
  }

}

Notice how ugly is this->cache->.... But it gets waay uglier when you try to make the application extensible trough plugins, because then you have to pass the application instance to its plugins, and you get $this->application->cache->...

With static functions:

interface CacheAdapter{
  abstract function get($k);
  abstract function set($k, $v);
}

class Cache{

  public static
    $ad;

  public function setAdapter(CacheAdapter $a){
    static::$ad = $ad;
  }

  public static function get($k){
    return static::$ad->get($k);
  }

  ...

}

class APCCache implements CacheAdapter{
  ...
}


class application{

  function __construct(){
    cache::setAdapter(new APCCache);
  }

  function whatever()    
    cache::add('blabla', 5);
    print cache::get('blabla');
  }

}

Here it looks nicer because you just call cache::get() everywhere. The disadvantage is that I loose the possibility to extend this class easily. But I've added a setAdapter method to make the class extensible to some point. I'm relying on the fact that I won't need to rewrite to replace the cache wrapper, ever, and that I won't need to run multiple application instances simultaneously (it's basically a site - and nobody works with two sites at the same time)

So, am doing it wrong?

2
  • 1
    I'm not a PHP developer, however -> is a common part of any "C-style" language and as such doesn't stop readability or make code look ugly. Oct 9, 2012 at 12:20
  • 1
    Can't you do $this->cache = $this->application->cache in the plugin constructor?
    – lortabac
    Oct 9, 2012 at 15:16

3 Answers 3

4

I think your intentions are good, but perhaps not the method. First, let's consider the obvious:

I'm relying on the fact that I won't need to rewrite to replace the cache wrapper, ever, and that I won't need to run multiple application instances simultaneously (it's basically a site - and nobody works with two sites at the same time)

These are assumptions. Sometimes assumptions are valid, sometimes they're not, and when they're not, they hurt. In general, removing flexibility is not a particularly great idea. Many large projects start out as "just give me a page where I can capture X", so don't be so hasty to predict the future.

More importantly, you may be going against standard practices. There's a lot to be said for conventions and standards. I'm far from a PHP expert, but from what I've seen, "$this->" is the kind of boilerplate that developers learn to ignore after a bit of practice. Is it really worthwhile going out of your way to shorten it by a little bit? If we take that bit for granted, only the plugin comment remains with "$this->application->cache". Is there any reason why a cache variable cannot be set up in this case to shorten it to "$this->cache"?

The above is just some general guidance, but actually what we should really be talking about here is dependency injection. Your various classes shouldn't be aware of what cache they are using, only that they have a cache and it can be accessed via $this->cache. By referring to a static implementation, you are breaking this rule, and just on that, it's a bad thing.

To summarise, I would say - try to shorten the code to $this->cache whereever you can, but no more. I believe that syntax is fairly idiomatic in PHP, and going beyond it is unnecessary.

Edit: I should add "don't sweat the small stuff" - I really think that you're over-thinking this problem (even if I've indulged you and added my rationale). This is a relatively trivial problem to be worrying about, and I'm sure there's better ways for you to spend your energy :)

0
2

If you think a primary language feature is ugly, you are going to end up writing very poor code trying to avoid it. Please just get over it or switch to another language that suits your style more.

Anything else only hurts us all.

1

Well it depends on how your class will be used and in what context.

With the risk of being downvoted by some, I recommend you take a look at 2 design patterns that may help you:

1
  • 3
    Your answer doesn't really explain anything. How are the two patterns relevant to the question, and how can the OP utilize them?
    – yannis
    Oct 9, 2012 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.