Posts: 19
Threads: 5
Joined: Jan 2016
Reputation:
1
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.
Posts: 1,062
Threads: 42
Joined: Mar 2015
Reputation:
73
I don't know about the ins and outs about implementing this idea, but for me, it seems like a great idea!
Paul.
Posts: 671
Threads: 17
Joined: Oct 2014
Reputation:
37
This idea means __magic. It might break third-party code.
Posts: 1,589
Threads: 1
Joined: Oct 2014
Reputation:
121
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.
Posts: 19
Threads: 5
Joined: Jan 2016
Reputation:
1
Narf is right.
I did not realised the facts.
My idea was bad.
Posts: 1,062
Threads: 42
Joined: Mar 2015
Reputation:
73
03-22-2016, 07:54 AM
(This post was last modified: 03-22-2016, 07:57 AM by PaulD.
Edit Reason: Added a question to Narf
)
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.
Posts: 1,062
Threads: 42
Joined: Mar 2015
Reputation:
73
Thanks! Appreciate the explanation.