Welcome Guest, Not a member yet? Register   Sign In
Clarification about MY_Controller and relative inheritances
#14

(04-18-2017, 04:31 PM)marksman Wrote: It will do the job, but I don't think it's the best practice.

Once again I do not understand the above statement. The pronoun's antecedent is vague in the extreme. To put that more clearly, what the heck is the "it" you're talking about?

marksman Wrote:Or simply use the framework's convention to autoload your files.
As I pointed out, the framework's "convention to autoload" design is not always adequate. For instance, consider two custom libraries.

File: application/libraries/CustomLib1.php
PHP Code:
class CustomLib1
{
 
   ...


And a second class that extends CustomLib1

File: application/libraries/CustomLib2.php
PHP Code:
class CustomLib2 extends CustomLib1
{
 public function 
__construct()
 {
 
       parent::__construct();
 }


If you're relying on the framework's "convention to autoload" and make the following call
PHP Code:
$this->load->library('CustomLib2'); 

You will receive a fatal error stating that Class 'CustomLib1' cannot be found.
To overcome this problem you have a couple options.

First option: Manually include the base class file before trying to load the child class.
PHP Code:
include APPPATH 'libraries/CustomLib1.php';
$this->load->library('CustomLib2'); 

Second option: Implement an autoloader.

The second option is what I prefer.

I'm happy to hear that you know spl_autoload_register is used to implement an autoloader. Big Grin
Reply


Messages In This Thread
RE: Clarification about MY_Controller and relative inheritances - by dave friend - 04-19-2017, 06:58 AM



Theme © iAndrew 2016 - Forum software by © MyBB