I wonder if there is any library for spl_autoload where one can just set all the folders and it will take care of loading them correctly.
Frameworks such as Zend Framework have components you can use (I mention ZF because it is loosely coupled) which wrap spl_autoload. But my experience is that if that's all the functionality you want to use, it's probably easier/quicker/lighter to write your own autoload function and register it.
Related
I'm building an MVC framework and I was thinking that most of the classes used in the making of an application are models. So since I know that every model is inside a folder I could just use the native __autoload() function to implement a "feature" (that obliviously you can enable and disable as well) that automatically loads a model (Lazy programming).
Is it good or should I discard this idea? If the latter: why?
If you're building a PHP framework, it might be worth looking into the PSR-0 standard for autoloading. I'm afraid I don't know much about it, but I believe a number of substantial frameworks and libraries have agreed to abide by it for interoperability. I believe that would include Symfony2, Propel2 and the next major version of Zend Framework.
There is absolutely nothing wrong with relying on the __autoload() function, as long as you have a consistent naming scheme.
In fact, it's often better to use autoloading - it stops you from including classes "just in case" you use them.
Zend framework is well known for loosely coupled components.
I would like to use XML-RPC from zend framework, is there any dependency for XML-RPC? Like if I had taken out XML-RPC folder off Zend Framework Library and try to instantiate RPC object, would it throw error?
Where can I find the proper way of separating component from the framework?
Thanks
I wrote a tool which takes ZF components and their dependencies so you can easily take just one (or several) component from ZF.
http://epic.codeutopia.net/pack/
It doesn't have the latest ZF release 1.11 (because I'm lazy), but 1.10.6 should work just fine.
You should never split single components off a framework or library independent from Zend Framework, or any other. Especially when using PHP there is also no performance reason, because with PHPs autoloading functionality it will always just include the files, which are requested.
You should literally be able to copy the XmlRpc folder from your copy of Zend Framework and use it in your own projects. The only dependency that I can see is in XmlRpc/Exception.php as it requires a file in the root directory of Zend/ (Exception.php) you could simply copy this file along with the XmlRpc folder keeping the directory structure the same and it should work....
My first question is why you would want to do that in the first place. It means that every time you upgrade Zend Framework you now need add a bunch of tooling to manage the removal of some components. One of the purposes of using a framework is so you don't have to manage a bunch of code. Removing parts of a framework is a step backwards IMHO. Disk space is cheap. Network transfer is cheap. If you are going to remove parts of a framework you should have a REALLY, REALLY good reason to do it.
I want to use a small library within the Zend Framework (simple_php_dom, for what it's worth).
Should I just stick it in library/, include it where I want to use it (like in a specific controller) like include('library/foo.php'); and have at it?
If not, how should I do it? What's the "Zend Framework" way of doing something like this?
Since the library doesnt support PEAR conventions its not really easy to hook it up to the autoloader, so i would just manually require_once it in the controller or model that uses it. If it was used extensively I might make a wrapper class to proxy calls through and autoload that (that class having the require_once).
I'm going to use Zend framework but just some tool of Zend like translate, date and cache. Can I use it as standalone class? My project has it own structure and I don't want use the whole Zend fw. If yes, which files should I include in my project? Is there a docs for using each Zend fw tool as standalone?
And remember, to use various Zend Framework components in another project, you just need to have the Zend library somewhere on your include_path. Copying the whole thing may seem overkill to use one component, but it's only disk space. Having those files there doesn't affect performance unless they are called upon. And this way, you don't have to sweat the dependencies, like Zend_Exception and its various component-specific subclasses.
So, for example, if you have a folder myapp/lib to contain your external libraries, you simply make sure that your include path contains that lib folder and copy the Zend folder into it as myapp/lib/Zend.
Then to use a component like Zend_Translate, all you have to do is something like the following:
require_once 'Zend/Translate.php';
$options = array(
// your options here
);
$translate = new Zend_Translate($options);
With some kind of autloading mechanism in place, you can avoid even the require_once call. Setting up autoloading is as easy as putting the following in some kind of common/bootstrap file:
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
Then any classes that follow the PEAR 1-class-1-file naming convention can be loaded without explicitly adding any require/include statements.
If disk-space really is a concern and you really don't want the whole Zend library, then you could investigate a packageizer, like Jani Hartikainen's Packageizer.
As an answer i could say Yes of course.
for example if u want to use Zend_Translate copy Translate.php and Translate folder to your library directory.
some times inside a class some other classes have been used. u have to copy them too. i find them by reading raised errors. ;)
I was wondering if anyone knew how to use some components of the Zend Framework without having to actually use the framework. For example, I would like to use their Zend_Validate components, but don't want the overhead of the framework as it's a small one-page script.
Can this be easily done, and if so, are there guides/tutorials on how to accomplish it?
Zend framework components are intentionally designed to be loosely couple from the framework itself.
The component structure of Zend
Framework is somewhat unique; each
component is designed with few
dependencies on other components. This
loosely coupled architecture allows
developers to use components
individually. We often call this a
"use-at-will" design. [from here]
Here's a tool for pulling out specific components and their dependencies to use in your application.
I've just grabbed the whole Zend package, and used pieces of it. It always seems I end up using more of it as time goes on, so I keep it up to date even if I'm not using some of the MVC stuff in one project or another. Holding on to the whole thing makes you not have to worry about the dependencies (and how they might change down the road).
Zend framework components while being loosely couple are still coupled. If you would to use Zend_Mail component for example - that would actually also require:
Zend_Mime
Zend_Exception
Zend_Validation
Zend_Validation will be loaded for the mere reason of validating email address domain.
So - best bet would be to include entire Zend library. By pulling only several components - you'll soon end up in "dependency hell" especially as API changes (though that doesn't happen too often).
Also - starting from version 2.0 you must use some auto-loader to load Zend components as all require calls will be removed from PHP classes.