Alternative to Registry: Static functions
Moderator: General Moderators
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
That's not really a singleton because you would get two different Smarty objects if you did:
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:
And:
But understand that you are adding the overhead of two function calls and two if checks for your convenience.
Code: Select all
$obj1 = new jmt_View();
$obj2 = new jmt_View();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();Code: Select all
$view = new jmt_View();
$view->setTemplate('template.tpl');
$view->displayPage();(#10850)
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:
vs:
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 
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();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();- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
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.
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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.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.
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)