I will use __autoload function in Codeigniter, but I'm not sure what is the best way.
I have read Phil Sturgeon's article and he uses at the bottom of config.php and
I asked to my friend, he uses in index.php
But I think they are not clean usage.
I want to use it in hook but I read this answer( last answer in the page ) How to add autoload-function to CodeIgniter?
and there is a negative vote on it.
Is to use in hook not good solution?
Codeigniter's autoload system is the easier way if you're using that framework: you just add the classname as index of an array and you're set.
Technically, it's not a "proper" autoloader, is a registry that checks if the class has already been instanciated in the static array of classes and, if not, adds it. In the way CI works it's efficient enough, and more important: is way easier for the user, compatible with any php version, and you don't need to code anything to implement it.
A proper autoloader using the __autoload() magic method, or better with spl_autoload_register() means you need to separate your loaded classes from what loads CI, and you need to implement it manually.
For future-ready applications check the PSR-0 standard which gives you indications on how to build a "standard" autoloader to be used to load classes in what's becoming the trend in PHP world: package management and distribution, similar to what other languages already do.
Recently I needed to do some modifications to a CakePHP project. Being completely new to Cake I was surprised to see how many helper functions the framework has included.
Of course some of them are really cool as they do all the heavy lifting, but what is the advantage of using e.g. the HTML helper to create markup in a view? I mean there is no difference between the <p> tags I write by hand or let cake print.
Or why should I use the file utility to delete a file? Can't I just use PHP's normal unlink function?
Are there any negative effects of using standard PHP functions instead of the CakePHP functions (besides sometimes more work)?
There are some functions in CakePHP that are there mostly for the functionality of the core modules. The file operations are an example. They have been unit tested so the CakePHP developers can use them to do things in their code, and have gifted them to us as users of their framework.
The View helpers are different. The HTML helper does a few things for you that make it a handy tool
They allow you to work with array to describe HTML tag attributes. I've used this to store HTML specific attributes far away from the view as settings for something, and it's easy then in the view to create those attributes.
They handle Routing of URLs for you. The HTML helper will create <a> links that use your applications routing. Should you change your routing then all your views are updated.
All the other helper classes have similar benefits.
I have a site which uses the library file lib.client.php which is stored in the php folder in my standard website root and contains a series of classes I have built.
I am going to have to require_once this library file in almost every single page, what is the best way I can implement this? Furthermore, should I be using some conbination of functions using __autoload to accomplish this?
Having seen a very similar question which touches on the issue but offers a very specific answer I can't really use, and looking a bit into the __autoload function, I want to get a more detailed answer for my requirements.
You use __autoload (or better: spl_autoload_register; or even better: some autoloader written by someone else) if:
you have many classes that are being used in different order in different files;
classes are spread across many files;
there is a strict way to tell which class is in which file.
In your case: just use require_once (or refactor, but I would not bother).
I would to create a function/class that would be available in model, view and controller in cakephp. How do I go about it? Where do I create it?
/app/config/bootstrap.php is meant for such functions, but as the manual says:
Be careful to maintain the MVC software design pattern when you add things to the bootstrap file: it might be tempting to place formatting functions there in order to use them in your controllers.
Resist the urge. You’ll be glad you did later on down the line.
If you have more extensive functions or classes you can use the vendors directory, especially if you use third party libraries.
The framework that I use (Zend) has an autoloader built it. I think this autoloader takes care of loading, not only Zend itself, but also any other libraries the developer may add to the project.
Is it normal practice for a framework to have an autoloader? Any that don't have?
Is it necessary to have an autoloader?
ZF's Autoloader will by default only load ZF's classes. It can (and is supposed to be) configured to also load your custom classes and you can also add additional Autoloader from other frameworks. ZF 2 will have a classmap autoloader in addition to the PSR-0 autoloader. See the Blog Post by ZF's Matthew Weier O'Phinney on Autoloading Benchmarks
Yes, it's common to have autoloaders because you dont want require statements all over your code. It's not strictly necessary though. The native way to achive autoloading is with spl_autoload_register.
Not it is not necessary, but it is very convenient. You don't need a hell of a lot of includes or requires, and units are only loaded when you need the class it contains. And that helps you stucture your code too, because incorrect naming will cause the autoloader to fail.
As stated by other members that not every framework has an Autoloader but I believe that every framework should.
If you can understand what a framework does than you should understand that there designed to be modular, being able to add and remove components easily, if you was to hard code all your libraries models, controllers etc then when you add new libraries you would have to recode for the library to work.
So that's the convenience part of the autoloader, the other part is the performance and resource management side of it, The base idea of the autoloader is to load the files only when required.
so on your index.php page you may need 5 libraries, but on your register.php you may only need 2 libraries, so that using an autoloader will save you 3 includes on your register page.
It also allows a better file structure as your components are separated and organized.
as you may be looking into placing an autoloader into your framework i strongly recommend you look at the PSR-0 standards that is currently in use in Zend and many other high end framework, the link can be found below:
http://groups.google.com/group/php-standards/web/psr-0-final-proposal
Yes, it is a common practice to include autoloader in the framework.
No, I can not think of any major framework without autoloader.
Yes, it is necessary if you want the framework to be flexible enough. It eases the addition of modules / classes to simply copying the files to the correct directory and invoking them from within the code. Autoloader just includes proper files instead of including all of the files (even if they are not needed at the moment).
Additionally, the way the classes are autoloaded can be determined completely by the autoloader mechanism, enforcing eg. proper directory structure or enforcing prior declaration of classes / files that can be autoloaded.
Many frameworks do have autoloaders, one that does not is CodeIgniter and you have to specify which classes you want to load all the time and which classes you want to load inside your controllers / models / views / libraries.
It isn't completely necessary, but it is nice because it generally enforces a file structure and you also don't have to worry about remembering to include files at the top of your scripts.
[Edit]
Apparently clarification is needed on my CodeIgniter statement. Yes there is an auto_load place that lets you specify the libraries/modules that you want to load automatically at the start of every script, and yes there is a loader library, but they are not the same as a real autoloader.
You have to specify which ones you want at the start, it doesn't know that you want to load the Pizza_Store modle until you use the custom
$this->load->model->('foo');
which is equivalent to
include 'application/models/foo.php';
$this->Foo = new foo();
But is not really the same as only having to call $this->foo = new Models_Foo(); and CodeIgniter knowing what you mean by it, which is what happens when an autoloader is used.
[Edit part deux]
Nowehere in the code does it say __autoload, spl_autoload or spl_autoload_register. This is because it maintains PHP 4 compatibility. It is just the way that the framework has been built, there is a false autoload as I stated above, but it does this for each "autoloaded" class:
foreach($autoloaded_class as $type=>$class){
if(is_application_class($class)){
include "application/{$type}/{$class}.php";
}
elseif(is_core($class)){
include "core/{$type}/{$class}.php";
}
}
$array['Foo'] = new Foo();
$controller($array);
Which is essentially calling:
include 'foo.php';
at the top of every file no matter if you need it or not.