Well I have a question. On Zend Framework We use a very interesting structure because if you intanciate a class then automatically Zend include the class file and after instanciate it (Class obviously allow to ge the directory's structure Class: abc_def_pqr then path is abc/def/pgr.php)
Does anyone have any idea???
Zend Framework uses spl_autoload_register to register a function that is called when a class is called but does not exist.
The function then does as you say, replace the underscores with directory separators and tries to include the file.
See Autoloading Classes but note using spl_autoload_register instead of __autoload allows for multiple autoload functions, which is better practice. Especially if being used in conjunction with ZF or external libraries.
Related
i've worked with older php frameworks such as magento 1.x line where class rewriting (other then by core/community/local overrides) is done using framework config and framework getModel() or getSingleton() like calls, that have the opportunity to check the config before loading the class.
in other words, it could very well be that the config is loading a class under a completely different name not just path.
my question is, how could rewrites like this be supported in a newer psr4/composer aware framework where classes are instantiated directly with the new keyword instead of some getModel() call?
you could perhaps put the config checks directly into the autoloader instead of using a method to get the class, but then what about composer libs, using their own autoloaders like vendor/autoload.php? just chaining the autoloaders would give a hit on the wrong class if the rewrite uses another class name and both exists.
some link or explanation on how to perform both overriding (as in core/community/local) and rewrites (as in config myClassName -> newClassName) using newer namespace, psr4 & composer aware techniques would be most welcomed.
I have read the Symfony documentation and I am still unsure of how to include a class. I want to include a Google Analytics PHP class into Symfony, but not sure on the following:
Where should it go? From the documentation I assume it should be under the vendor directory?
How would I then include the class? Ideally I would only like to include it where necessary (ie when I need it).
How would I then utilise the class?
Cheers
Adam
Use the app/autoload.php file to configure your autoload mechanism.
If your class doesn't follow PEAR style conventions or PSR-0 standards then you should plainly require it (as is the case with Swiftmailer in the same file)
If your class follows PEAR conventions then you should use registerPrefixes method else, if your class follows PSR-0 standard (maybe not the case), you should use the registerNamespaces method
I'm actually learning Symfony and I don't understand the fact of including classes. Can I include a standalone class - this means, not designed for a framework - and then use it - let's say, Amazon S3 class -?
Just put your class in wherever lib (sub)directory it suits you the best. It will be autoloaded by the framework.
If you don't want it to be automatically autoloaded than put it somewhere in the lib/vendor directory.
If your class is reusable than it's the best to put it in a plugin.
I'm guessing this line registers the autoload function, which in turn loads needed Zend classes.
Zend_Loader::registerAutoload();
My question: is this line meant to be used in applications that call some zend components but aren't fully zend applications? or is needed also in applications that are fully zend and use zend MVC?
Well, first we should note that Zend_Loader::registerAutload() is deprecated (since 1.8.0). Better is:
Zend_Loader_Autoload::getInstance();
What this does is register an SPL __autoload($classname) function that attempts load classes when they are called for but not-yet-loaded. The default behavior of this autoloader in a non-framework application is to map a class name to a file name (relative to the currently defined include_path) and include() that file in the hopes that the requested class will be defined there.
The specific mapping uses the PEAR 1-class-1-file convention in which a class named something like My_ComponentName_ClassName will reside in the file My/ComponentName/ClassName.php.
See this answer for more details.
I am writing a add-on module which is integrated with an existing PHP application; Because I am using a MVC pattern, and which may requires lot of inclusion of classes (which might not be used at all depending on the action of the user), I decide to use autoloading of classes.
However, I have to ensure that the autoload function does not interferes with the normal operations of the existing applications.
Does autoload only kicks in if a class name is not defined?
Say I have to write another module which uses its own autoload functions (say, I have an autoload for a module, since they each reside in their own folder), how do I differentiate which module is it for?
For #2, I thought of 2 options. Either prefix the class name with the module name (Such as 'MyNewModule_View_Default' and 'AnotherModule_View_Default'), or use file_exists to check the include file exists.
Other suggestions are welcomed too!
Just check if the class that is to be loaded already exists with class_exists() before actually loading it in your autoloader implementation. Especially if you have multiple registered autoloaders (see 2).
You can specify multiple autoloaders in a stack via spl_autoload_register(). The registered functions are executed in the order in which they where registered until the class is successfully loaded. You can specify different autoloaders for different modules for example. Or you can do a namespacing approach like in the Zend_Framework, if you have control over class names.
Yes, autoloader is only called when class name is not found.
Usually you'd check the class' namespace (pre 5.3 you use pseudo-namespaces, usually separated by an underscore). So your autoloader would only load classes that are under the namespace(s) of your application.