Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Eric Hogue's Blog:
Late Static Binding
Apr 22, 2011 @ 14:14:34

Eric Hogue has a recent post to his blog looking at one of the more tricky aspects of the latest versions of PHP (the 5.3.x series) - late static binding. In a nutshell, late static binding (LSB) lets static classes and methods work more correctly than before. Eric gets into a bit more detail than that:

It came out almost 2 years ago, but it to me that many programmers around me have no idea about it. Myself, I have learned about it around 6 months ago. The PHP documentation defines late static binding as a way to "reference the called class in a context of static inheritance." This definition didn't really help me the first time I read it. Fortunately, there are more explanations in the documentation, and there are good examples. If you haven't, you should read it.

To clarify, he includes a code snippet showing the use of the "static" keyword to correctly reference a static method. He also includes in interesting bit about when's a good time to use it.

tagged: late static binding lsb tutorial explaination

Link:

Brandon Savage's Blog:
A Lesson In Static Methods And Late Static Binding
Apr 12, 2010 @ 16:10:51

Brandon Savage in his frustrations with the Zend Framework and the "self" keyword in PHP has written up a new post showing how you can use late static binding to work around it.

he problem is, when extended, My_Auth::getInstance() still returns an instance of Zend_Auth. The solution was to duplicate the static method in my My_Auth class, which worked properly. What did I get as a return value? Zend_Auth [...] Why didn’t I get an instance of My_Auth instead of Zend_Auth? Well, that’s because PHP determines the meaning of the self keyword at compile time, meaning that when you call a function that makes use of it later, you’ll get whatever it’s been defined to mean when it was compiled.

To remedy the situation he uses late static binding (in PHP 5.3+) by using the "static" keyword like you would use "self" to refer correctly to the current class, not the class it sees at runtime.

tagged: static method latestaticbinding lsb method

Link:

Jordi Boggiano's Blog:
Multiton base class
Dec 30, 2008 @ 17:17:49

In this recent post Jordi Boggiano looks at a different sort of design pattern - a sort of extension of the Singleton pattern: Multition.

While I like the Singleton pattern every now and then, I prefer the flexibility that the Multiton potentially offers, and well it's just an extended version of the Singleton, so it's "compatible" with the Singleton model. Anyway, to the point, PHP5.3 is coming, and with Late Static Binding you can do a base Multiton (or Singleton if you insist), which wasn't possible before. Now I like this very much because you can simply extend it rather than rewriting those (few, I know, but still) lines each time.

Included in the post is an example of the design pattern showing how to create its structure in the class and use it to grab the same or unique instances (defined with an ID).

tagged: multiton base class singleton php5 latestaticbinding lsb

Link:

IBM developerWorks:
What's new in PHP V5.3, Part 1: Changes to the object interface
Nov 12, 2008 @ 17:13:30

John Mertic has put together a what's new list in the upcoming PHP 5.3 release:

PHP V5.3 is set to be released by the end of 2008, and many of the new features in this release have been in the planning stages for a few years. Originally touted as "PHP V6 without native Unicode support," PHP V5.3 has been developed into a feature-rich upgrade to the PHP V5 line. [...] In this "What's new in PHP V5.3" series, we'll look at these new V5.3 features, and see how they are used and how they can be used in your Web application.

In this first part of the series he talks about:

  • Improved static method and member handling
  • The _callStatic() magic method
  • Dynamic static calls
  • Late static binding
  • Standard PHP Library
  • Circular garbage collection
tagged: object interface php5 whatsnew static lsb spl garbage collection

Link:


Trending Topics: