NYCPHP Meetup

NYPHP.org

[nycphp-talk] Static Methods Usage

Daniel Krook krook at us.ibm.com
Fri Jan 6 15:25:09 EST 2006


Cliff & Andrew,

> On Fri, Jan 06, 2006 at 02:07:30PM -0500, Cliff Hirsch wrote:
> > As I refactor my code yet again, I am torn about whether to make
> > wide-spread use of static methods (in PHP 5) when appropriate.
...
> 
> If all your methods are static then you're OO code is not too much
> different than prcocedural code. So, the answer is to use them when
> appropriate.


I disagree that extensive use of static methods makes your OOP code 
equivalent to procedural code (meaning that you'd want to avoid it). While 
it's true that you can essentially just create function namespaces when 
you do this, conceptually you're still binding operations under an object 
with distinct responsibilities.

I tend to use static methods for read only operations on particular other 
objects.  For example:
$dogs = DogManager::getLatestAdoptableDogs();

foreach ($dogs as $dog) {
        echo $dog->firstName;
}

When I need to write to a database I like to use a singleton* instance of 
that manager to make sure that only one update is happening on a shared 
connection at any given time.  For example:
$dog = new Dog($someData);

$dm = DogManager::getInstance();
$dogId = $dm->saveDog($dog);
$dm->createAdoptionRecord($dogId, $humanId);

In any case, I tend to do much of this without consideration for 
performance.  Logically organized code with intuitive responsibilities 
takes higher precedence in my mind, so I would encourage you to use static 
methods in your new code.

HTH,
-Dan


*Information on PHP 5 Singletons: 
http://www.sitepoint.com/article/coming-soon-webserver-near/9




Daniel Krook, Content Tools Developer
Global Production Services - Tools, ibm.com

http://bluepages.redirect.webahead.ibm.com/
http://blogpages.redirect.webahead.ibm.com/
http://bookmarks.redirect.webahead.ibm.com/



More information about the talk mailing list