Page 1 of 4

Service Locator

Posted: Mon Jul 17, 2006 11:48 am
by Luke
Would this be considered a service locator? I am trying to eliminate 100 manual includes

Code: Select all

class Registry{
        var $instances;
        function register($name) {
			@include_once(CORE_INCLUDE_PATH . 'classes/' . $name . '.inc.php');
            $this->instances[$name] = new {$name};
        }
        function &get($name) {
            return $this->instances[$name];
        }
}
I know this isn't correct... because it doesn't work. But am i on the right track?

Re: Service Locator

Posted: Mon Jul 17, 2006 12:23 pm
by Christopher
Yes, a simple and generic one in that it does not also know about parameters needed for construction.

You might want to add Lazy Loading by doing the instantiation on the first get instead of at register. It is also nice to be able to pass a parameter when you register classes. If you look at your design, your really don't need to register() every class, only those with args, because you pass name to both. Something like this:

Code: Select all

class Registry{
        var $instances;
        var $args;
        function register($name, $args=array()) {
        $this->args[$name] = $args;
        }
        function &get($name) {
            if (! isset($this->instances[$name])) {
                if (! class_exists($name])) {
                    include(CORE_INCLUDE_PATH . 'classes/' . $name . '.inc.php');
                }
                $this->instances[$name] =& new {$name} ($this->args[$name]);
            }
            return $this->instances[$name];
        }
}
PHP4 code.

Posted: Mon Jul 17, 2006 12:31 pm
by daedalus__
What is that.. thing.. doing? lol

Is that like.. an autoloader?

Posted: Mon Jul 17, 2006 12:50 pm
by Todd_Z
I use the __autoload function in php5.

It simply searches the specified directory and all sub dirs, that way any class you need can be placed in a file called CLASS_NAME.php and then its auto loaded. Easy.

... idk the php4 fix ...

Posted: Mon Jul 17, 2006 12:53 pm
by Luke
That's cool.

Note to arborint:
I'm trying to make sense of all of this stuff so I can really make use of your code. I plan on using your skeleton once I figure out exactly how it works

Posted: Mon Jul 17, 2006 1:01 pm
by daedalus__
ninja you are on php4?

Posted: Mon Jul 17, 2006 1:31 pm
by Luke
yes

EDIT: I decided to go with php5

Posted: Mon Jul 17, 2006 1:55 pm
by Christopher
The thing about a real Service Locator (as opposed to a class loader) is that a Service Locator can create fully instansiated classes -- meaning passing (and even creating) the parameters for the constructor.

You can certainly use that get() method above as/in the __autoload() function as well -- just as Todd_Z says -- so it can do double duty.

So here is the programming project, because I like where this thing is going. You know, it is often better to start building from scratch to get clean solution). I am going to throw out some requirements and we will see if we can code a nice Service Locator.

- Be able to register a list of parameters to pass to the constructor (we may need eval() for that)

- Be able to specify that a parameter is another "registered" object that we get() an instance of to pass as a parameter (think of a Model object that need a DB connection object passed to its constructor)

- Allow get() to get objects as singletons or multiple instances

- Allow instances to be accessed by a name that is not the class name.

- Have the class name to file name mapping be configurable when you create the Service Locator (not everyone likes your naming style ;))

- Keeping Lazy Loading so that if we never get() the class it is never instansiated (say an error redirect occurs before the get()).

- Make it so it is easy to use as __autoload() or spl_autoload().

- How does PHP5 make our job easier or the interface nicer?

- Do we want to add a settable list of paths that get() will search -- like Todd_Z does as an option?

Posted: Tue Jul 18, 2006 5:52 pm
by Luke
Alright... I think I am ready to start on this. I'm not really sure where to begin, but I will start researching ways to accomplish this.

Posted: Tue Jul 18, 2006 7:15 pm
by Christopher
I think start with the first two. Take a look at the code I posted above and throw out some ideas about how to allow multiple parameters and for paramenters to be other registered objects.

Posted: Tue Jul 18, 2006 7:32 pm
by Luke
I've always heard that one should avoid eval at all costs. Does this apply only to situations involving user input?

Posted: Tue Jul 18, 2006 7:40 pm
by Weirdan
It's just a maxim... after all, eval is there for a reason.

Posted: Tue Jul 18, 2006 7:42 pm
by Luke
a maxim?

Posted: Tue Jul 18, 2006 7:45 pm
by Weirdan

Posted: Tue Jul 18, 2006 7:57 pm
by Luke
I'm getting this error:
error wrote:Parse error: parse error, unexpected '{', expecting T_STRING or T_VARIABLE or '$' in c:\wamp\www\module_admin\classes\Registry.inc.php on line 13
for this line:

Code: Select all

$this->instances[$name] = new {$name} ($this->args[$name]);