Alternative to Registry: Static functions

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

User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Yeah, that's a Singleton.

In a way, the solution presented below is a slightly more configurable Singleton when you don't copy it.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I prefer to stick the instance inside a registry so I don't have to bother adding a method for fetching the instance each time..
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

That's not really a singleton because you would get two different Smarty objects if you did:

Code: Select all

$obj1 = new jmt_View();
$obj2 = new jmt_View();
So there can be more than one "jmt_View" objects. If you made Smarty a static then you'd have a Singleton because no matter how many jmt_View objects you created they would all have the same templates. I doubt in real use that you really need a Singleton anyway.

What you have there is a static convenience function. Convenience can be a good thing or a code smell. I am not sure what, in your mind, the difference between:

Code: Select all

jmt_View::instance()->setTemplate('template.tpl');
jmt_View::instance()->displayPage();
And:

Code: Select all

$view = new jmt_View();
$view->setTemplate('template.tpl');
$view->displayPage();
But understand that you are adding the overhead of two function calls and two if checks for your convenience.
(#10850)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I needed a solution to fit the problem of allowing the calling of an object on any scope, as well as still being able to create multiple instances of the same object :)

It is convinient like you say, if during any stage of development I want to use the jmt_View object, I can call jmt_View::instance(), if I want to use another object (jmt_Display for example) I can simply call upon that: jmt_Display::instance()

Registry sounds like it may have been the better option; however what is the overhead of a Registry static class vs a static method in every class? :)

At the moment, I see the differences being:

Code: Select all

jmt_View::instance()->setTemplate('template.tpl');
jmt_View::instance()->displayPage();
//or..
$view = jmt_View::instance();
$view->setTemplate('template.tpl');
$view->displayPage();
vs:

Code: Select all

Regsitry::instance('jmt_View')->setTemplate('template.tpl');
Registry::instance('jmt_View')->displayPage();
//or again..
$view = Registry::instance('jmt_View');
$view->setTemplate('template.tpl');
$view->displayPage();
At the moment, I'm not fussed about overhead's - I'm still just getting the application in to a stage where I am satisfied it does what I (and the client) wants, but after it is 'working' I shall be looking into overheads and optimisation. :) Again 'however' I am tempted to just use a registry class as the functionality is all in one place, and not duplicated in every class definition.. so I think I have just convinced myself to change and use that instead :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

If you put it that way, main benefit of using a registry is that now you don't have to implement the Singleton code on every single object that you want to make easily accessible: only the Registry needs to be a Singleton (if it is at all, seems like you're using static functions).

Here's some food for thought: why not try passing around the View component by parameters when it's needed? If that proves difficult, that may be indicative of a bigger problem: seperation of Model vs. View.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Ambush Commander wrote:Here's some food for thought: why not try passing around the View component by parameters when it's needed? If that proves difficult, that may be indicative of a bigger problem: seperation of Model vs. View.
I agree with this wholeheartedly. Actually creating object and passing them makes dependencies explicit -- and that will help you maintain those dependencies in the long run.

There seems to be a flurry of static classes/methods being used recently -- there have been several discussions in the Zend Framework lists. Obviously there is a new group of programmers being exposed to PHP5.

I would suggest programming it using normal OO practices and then resort to Registries and statics when the design forces you to. My concern is programmers dragging procedural practices into an OO design due to lack of knowledge -- as opposed to a well thought thorugh design that is making a trade-off from experience, for example by adding a Singleton.
(#10850)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I inadvertently went with the arguments choice, I just looked at the classes and thought to myself "er.. why am I doing this?"
Post Reply