Syntactic sugar in PHP with static functions

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

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

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.

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:

  • Singleton – http://en.wikipedia.org/wiki/Singleton_pattern
  • Monostate – http://c2.com/cgi/wiki?MonostatePattern

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật