curiosity4834
curiosity4834

Reputation: 73

What is the best solution( especially for performance and clean code ) to use __autoload in codeigniter?

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?

Upvotes: 0

Views: 294

Answers (1)

Damien Pirsy
Damien Pirsy

Reputation: 25445

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.

Upvotes: 1

Related Questions