News Feed
Sections




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

Bence Eros' Blog:
Life without static in PHP
December 20, 2010 @ 13:17:03

In this new post to his blog Bence Eros shares some of the frustrations and issues he's had when dealing with static methods and properties in his applications and how, with a bit of re-engineering, you might be able to have "life without static".

The problem with static members in PHP is the poor initialisation capabilities. The initial value of a static property can only be a literal or a named constant. [...] The same problem exists for non-static properties too, but the constructor is a dedicated place to initialize non-static properties. But since we don't have Java-like static constructors in PHP there is no place to do static property initialization. In a lot of cases people do it by putting the assignment statements after the body of the class, but this method is very ugly.

An alternate method he suggests is using a singleton to initialize and grab the value of a class value. He gives some sample code to show how it might be done, but warns that it might not be the right way to do things. It has "significant disadvantages" that could cause trouble down the road (for one, singletons make it difficult to unit test).

0 comments voice your opinion now!
static class tutorial singleton replace


blog comments powered by Disqus

Similar Posts

DevShed: Abstracting Database Access Using Polymorphism with Objects in PHP 5

Pablo Viquez' Blog: How to use json_encode with ISO-8859-1 data – Part2

DevShed: Parsing Child Nodes with the DOM XML extension in PHP 5

PHPWomen.org: One way to handle concurrency in a multi-user web app

SEO Expert Blog: Amazon Wish Lists with SimpleXML


Community Events

Don't see your event here?
Let us know!


release install application community interview series framework configure language symfony2 laravel performance opinion example introduction php7 conference api podcast library

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework