Welcome Guest, Not a member yet? Register   Sign In
Loading models in controllers by extending a custom core class.
#2

It seems to me that the main problem you are trying to solve is

(05-25-2019, 03:09 AM)bartMommens Wrote: I don't want to repeat code to check if the model file exists, a particular function exists and that the class has already loaded.

Yes, all of those concerns are valid, but you have added a bunch of complexity to solve problems that a developer will find immediately during the code writing/testing process. A non-existent (i.e. misnamed) class will error as soon as an attempt is made to load it. A valid class that is not instantiated will also error on any attempt to use it. A non-existent method is also immediately known to the developer. Resolving those issues is a development task, not a runtime task.

CodeIgniter already handles previously loaded models and libraries in ways that are quickly apparent during development.

With regard to files in subfolders. Constants are a simple, high-performance solution, e.g.

PHP Code:
$this->load->model(SOME_FOLDER 'some_model'); 

Where the constant would be in some config file like so

PHP Code:
define(SOME_FOLDERAPPPATH "models/somefolder/"); 

You could define 'some_model' as a constant too if you're in the habit of renaming them often. But any decent IDE has a good refactoring feature or, at the very least, an easy-to-use global search and replace. For code maintenance, readability cannot be understated, so a hard-coded file name is preferred over a constant in my opinion.

In summary, I feel that the existing model loading tools are wholly adequate and that loading should happen at the time and place where it makes the most sense. That time and place determination happens on a case-by-case basis and could be done via /config/autoload.php, in a controller's constructor (which might be in an extended class), or in a controller method.
Reply


Messages In This Thread
RE: Loading models in controllers by extending a custom core class. - by dave friend - 05-25-2019, 08:37 AM



Theme © iAndrew 2016 - Forum software by © MyBB