简体   繁体   中英

Php, Singletons Autoloading

I've made standart autoloader for my classes using spl_autoload_register. But I have problem. spl_autoload_register makeing work only when we instancing a class using new operator. So, when i have: $singleton = Name\\Singleton::get_instance() in code, autoloader doesn't work and i should include this file manually. How I can resolve this problem?

This should not be the case, classes are autoloaded whenever they're needed. Try to make sure your namespace/classname is correct if you're using namespaces. You can do var_dump(class_exists('Name\\Singleton')); as well to check whether the class exists or not, this will for sure use the autoloader.

Regardless weather your creating an object or accessing a static class they will be loaded.

your example will work file as the class is loaded before the resolution operator :: therefore its within scope.

You should look at the following link to help you get your autoloader just right aswell.

http://groups.google.com/group/php-standards/web/psr-0-final-proposal

and

http://gist.github.com/221634

As the namespace proposal is stated that every root namespace should be a vendor \\<Vendor Name>\\(<Namespace>\\)*<Class Name> you would do the following (With the GitHub Version):

   $App = new SplClassLoader('MyApp', '/myapp');
   $App->register();

Update to help you with name spaces a little more.

Take this example

namespace MyApp\Classes;

class SomeClass
{
    public function __construct()
    {
        $Doctrine = Doctrine\Common\EntityManager::Singleton();
    }
}

The Doctrine call would look in the namespace MyApp\\Classes\\Doctrine\\Common for the EntityManager class.

What you should de is prepend it with a \\ as your Outside the Doctrine namespace you should always specify a \\ to say that its not within the current namespace.

In your post this may be the reason what its not working.

Actualy namspaces came to mess up a bit.

If you got a singleton you probably is doing something like this:

self::$_instance = new self();

that will mess up because it will throw a stdClass to your autoloader for some wicked reason, and i can,t even understand why or find a solution

edit: Actualy I was wrong on this answer, i really used a stdClass on the constructor so I had to import it form the global space with the use function, actualy ive used a cast of (object), either way i guess the global space should be always available.

After i imported the stdClass the singleton just worked

I'd agree with @Seldaek and would look into the namespace you're calling the method from. Which namespace are you in when you call the static method? If you're in the 'Name' namespace you don't need to preface the class name with the namespace name. If you're in another namespace you need to preface the method call with a '\\' to go back to the root namespace, like $singleton = \\Name\\Singleton::get_instance() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM