static classes or singletons

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

Post Reply
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

static classes or singletons

Post by andre_c »

I have a few classes in the Services layer that i want to implement:
This is 2 of them:

Code: Select all

<?
class UserManager {
  public function getUserById( $id ) {
    ...
  }

  public function getAdsByUserId( $id ) {
    ...
  }

  // etc
}

class PublicationManager {
  public function getPublicationById( $id ) {
    ...
  }
  
  public function getPublicationsByClientId( $client_id ) {
    ...
  }
  
  // etc
}

?>
do you recommend using a static class, singletons for each class, or simply creating a new instance as needed (since there's no properties to store)?
User avatar
SystemWisdom
Forum Commoner
Posts: 69
Joined: Sat Mar 26, 2005 5:54 pm
Location: A Canadian South of the 49th Parallel

Post by SystemWisdom »

Right away im thinking Singletons, since it seems you are only using the class for organizational/scope purposes.. Instantiating an object would be pointless, and you would have to add a global statement to any other function that wants to access its methods..

Personally, I would just use scope resolution:

Code: Select all

UserManager::getUserById( 1000 );
I like it better because it is clear which class the function belongs too..

That's just my opinion..
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Small point: these look like Finders?

Instantiate an object. You might need to configure it at instantiation. With statically called methods, the Finder has no state (it doesn't exist as an object, of course).

Code: Select all

$user_finder =& new UserFinderFromDatabase($db);

// or:
$user_finder =& new UserFinderFromFile('path/to/users_file');

// in either case:
$user =& $user_finder->findById($id);
Avoid singletons if you can. Avoid globals at all costs. Globals are evil. Singletons slightly less so. Here's an article on Registry.
Last edited by McGruff on Sat Aug 06, 2005 10:08 pm, edited 1 time in total.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

i'm leaning towards instantiating them every time, since i don't like singletons either and i guess they might end up having configuration properties (like McGruff says).

Are the ampersands still necessary in PHP5?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Ampersands: no not in php5.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

Ok Thanks,
I ended up implementing the classes the way that McGruff said.
It's an extra step but it seems to be the best since i can configure the objects if they need to be configured and instantiation doesn't really cost me much.

Static classes seem kind of similar to using global objects... i don't know, i get that same feeling with singletons too...
Post Reply