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.
<?
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)?
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..
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).
$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.
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).
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...