How Many Static Methods is Too Many For One Class?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

fredrik
Forum Newbie
Posts: 13
Joined: Sun Nov 04, 2007 4:41 am

Re: How Many Static Methods is Too Many For One Class?

Post by fredrik »

allspiritseve wrote:
How Many Static Methods is Too Many For One Class?
1
So true, couldn't have said it better myself.

Edit: I never, ever - use static methods, PHP has normal functions for this if you really really need it - but honestly havn't caught myself using either static methods or normal functions for a looong time, and with the introduction of closures in php5.3 I see even less reason to use them.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: How Many Static Methods is Too Many For One Class?

Post by Jenk »

Whilst static methods are really nothing more than functions, it's nice to have them collected. You might have several methods that convert, for example, but you don't want to create a load of functions like convertToObjectA(), convertToObjectB() etc, when you could have ClassA::convert(), ClassB::convert() etc.

Another good example for collecting would be hashing (ignoring PHP's built in functions for now) Hash::md5(), Hash::SHA256() and so on.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: How Many Static Methods is Too Many For One Class?

Post by allspiritseve »

gabriel1836 wrote:@allspiritseve:

If you look at the code blocks in the original post, you'll find these four methods:

Code: Select all

 
abstract public static function get($params);
abstract public static function getCollection($params);
abstract public static function create($params);
abstract public static function factory($row);
 
The final block of code shows how each of these functions is implemented. The factory method is used only in situations where I need to implement some form of class table inheritance where I have types of some sort of object with varying properties and behaviors beyond the base set.

Additionally, I thought some more about the create() method and I think that I might just remove it in favor of use the save() instance method to insert to the database if the necessary records don't already exist otherwise save() would just update the database. Does anyone see any issues with that?
Sorry I haven't been back to this thread in a while...

I am not sure I completely understand what your code is doing. What is an artifact? The classes above make me think you're implementing a gateway, but you're delegating to a gateway, so I guess I don't see the point to any of these classes. Also, I tend to stay away from a "base set" of behavior or properties. Could you explain to me what exactly your application is going to do? What domain objects are you working with?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: How Many Static Methods is Too Many For One Class?

Post by josh »

I personally use some static methods. Mostly this is setting database adapters or layout settings to registry. I also use a static method to get a hold of fully configured data mappers from my models / view helpers / controllers, etc.. Usually this is self encapsulated in a class method though to decrease the static coupling. I'm also now doing stuff like:

Code: Select all

 
foreach( $array as $model )
{
            Ne8::assertInstance( $model, 'Ne8_Model' );
 }
 
Which would throw an exception if the assertion failed ( in development, or would just return; in production ). I don't think static methods are "bad", but you have to realize the trade offs you're making by using them, you loose all the benefits objects give you, theres no law for using 100% objects vs 100% procedural either, some great systems were written on procedural code which we tend to forget sometimes. Objects have proven more effective for certain applications only.
Post Reply