Service Locator

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
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Service Locator

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Service Locator

Post 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.
(#10850)
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

What is that.. thing.. doing? lol

Is that like.. an autoloader?
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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 ...
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

ninja you are on php4?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yes

EDIT: I decided to go with php5
Last edited by Luke on Tue Aug 08, 2006 8:11 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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?
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I've always heard that one should avoid eval at all costs. Does this apply only to situations involving user input?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

It's just a maxim... after all, eval is there for a reason.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

a maxim?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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]);
Post Reply