CodeIgniter Forums
Autoload idea - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forum-6.html)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forum-17.html)
+--- Thread: Autoload idea (/thread-64758.html)



Autoload idea - Sezu - 03-22-2016

Hi.

I have this kind of code in many places ....

$this->load->library('some_library');
$this->some_library->some_method('some_string');


It would by good to make some sort of "magic" autoload and remove need of the first line with loading library.

$this->some_library->some_method('some_string'); //autoloader could be able to try to find "some library" if it exists on default paths.


What do you think about it?

Thanks for reply.


RE: Autoload idea - PaulD - 03-22-2016

I don't know about the ins and outs about implementing this idea, but for me, it seems like a great idea!

Paul.


RE: Autoload idea - ivantcholakov - 03-22-2016

This idea means __magic. It might break third-party code.


RE: Autoload idea - Narf - 03-22-2016

It is technically possible we're taking into account only what you've suggested, but there are two major problems with that:

- How do we know you wanted to load a library instead of a model? (technically, our naming convention suggests that models should have the '_model' suffix to avoid name collisions, but that's not enforced)
- How do we know you wanted to load anything at all? What if you just made a typo in the property name you're trying to access?

And then there are overall design concerns:

- You've called it "magic" yourself; magic is bad.
- Using $this->something to access a specific type of object is a legacy solution, one that has been necessary in the PHP4 era. With spl_autoload_register(), that's no longer the case and we shouldn't build upon it.


RE: Autoload idea - Sezu - 03-22-2016

Narf is right. 

I did not realised the facts. 

My idea was bad.


RE: Autoload idea - PaulD - 03-22-2016

Yes, following Narfs points he is right, it is not a good idea.

An interesting suggestion though Sezu, even if, under scrutiny, it didn't pan out.

Paul.

@Narf
Could you expand a bit on that comment about using $this->something to access a specific type? Or perhaps suggest a link for me to read about it? Thank you.


RE: Autoload idea - Sezu - 03-22-2016

When you use something like:

$this->username = 'gogo';

You should declare property "username" in the class before use it. If you don't, PHP sets it "on the fly". It's called magic. And it is very hard to debug. I personaly hate magic. Now I really don't uderstand why did I sugessted this.

Using term "Legacy" made me laugh Big Grin


RE: Autoload idea - Narf - 03-22-2016

(03-22-2016, 07:54 AM)PaulD Wrote: Yes, following Narfs points he is right, it is not a good idea.

An interesting suggestion though Sezu, even if, under scrutiny, it didn't pan out.

Paul.

@Narf
Could you expand a bit on that comment about using $this->something to access a specific type? Or perhaps suggest a link for me to read about it? Thank you.

There's nothing to link to ... it's not something common, just how CI has solved this one particular problem.

It would be very annoying if you had to do a require_once() AND $something = Something($config); every time you wanted to load a library, so someone came up with $this->load->library('something') and $this->load->model('something_model'), each assigning the appropriate class instance to a CI_Controller property.
But that was the easiest way back then, not now.

Today, with PHP's lazy auto-loading, you can just do $something = new Something($param1, $param2); and it's all under your control - no need for a dedicated loader library, no need to use a class property if you don't want to, no limitations about constructor parameters, etc.
Any new auto-loading solutions should be built that way.


RE: Autoload idea - PaulD - 03-22-2016

Thanks! Appreciate the explanation.