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

(This post was last modified: 04-19-2017, 04:54 PM by marksman.)

(04-19-2017, 06:58 AM)dave friend Wrote:
(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

Maybe you have poor understanding.

I'm talking about your post, It's about including file manually,

If using framework's convention in your case is not applicable then create your own convention on top of framework's convention.

I don't have much time to explain it and argue with you.

Quote:I just want to say that your way will work but IF you will be working with someone else (with a team, big projects, real world) your way will be rejected in code review phase of your project for sure Smile

No offense but just saying the reality. If you still want your way then do it your way Smile

its your choice if you want to keep it wrong or just do the best practices.
God Bless CI Contributors Smile
Reply


Messages In This Thread
RE: Clarification about MY_Controller and relative inheritances - by marksman - 04-19-2017, 04:53 PM



Theme © iAndrew 2016 - Forum software by © MyBB