Welcome Guest, Not a member yet? Register   Sign In
Library Extending Issues
#1

[eluser]wowdezign[/eluser]
OK, what am I missing here?

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Calendar extends CI_Calendar{
    public function __construct(){
        parent::__construct();
    }

    public function testMe(){
        echo"Working!!!!";
        die();
    }
}

if I call this from my controller like:

Code:
$this->load->library('calendar','','cal1');
$this->load->library('calendar','','cal2');
$this->load->library('calendar','','cal3');
$this->cal1->testMe();

it works fine.

But when I try to extend the MY_Calendar class with:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH.'libraries/MY_Calendar'.EXT);
class Availability_Calendar extends MY_Calendar{
    public function __construct(){
        parent::__construct();
    }
}

This call doesn't work.

Code:
$this->load->library('Availability_Calendar','','cal1');
$this->load->library('Availability_Calendar','','cal2');
$this->load->library('Availability_Calendar','','cal3');
$this->cal1->testMe();

I get this error:

Fatal error: Class 'CI_Calendar' not found in C:\path\to\my\application\libraries\MY_Calendar.php on line 3

I don't get it. Why can't I extend an extended class? Confusedhut:
#2

[eluser]dmorin[/eluser]
When you extend the class using MY_, CI knows to look for it and loads it automatically, even though you load the base class, not the extended one. In your double extended example, you're not loading the base anymore, you're directly loading the extended one. So all you need to do is change your line #2 in Availability_Calendar to be:

Code:
require_once(BASEPATH.'libraries/Calendar'.EXT);
require_once(APPPATH.'libraries/MY_Calendar'.EXT);
#3

[eluser]wowdezign[/eluser]
Ah! So are you saying that any time I extend a class more than once, every descendant must load all parents or it doesn't get the code?
#4

[eluser]davidbehler[/eluser]
Exactly.

If you extend a CI class using the 'MY_'-prefix, CI automatically loads the extended CI class before loading yours and creating the instance. But if you extend that class again and try to create an instance of the new class, it fails because the originally extended CI class was not loaded. Doing what dmorin proposed should fix your problem.
#5

[eluser]wowdezign[/eluser]
And, it did in fact solve the problem.

So is it possible for me to to create my own library that loads the Calendar class and "has a" calendar instead of "is a" calendar?

I would need to have multiple instances on my object so how do I set a reference to an instance? Like normal?

Code:
class myClass{

  private $ci;
  
  public function __construct(){
    parent::__construct();
    $this->ci->load->library('calendar','','cal1');
    $this->ci->load->library('calendar','','cal2');
    $this->ci->load->library('calendar','','cal3');
  }

  public function someMethod(){
    return $this->ci->cal1->method_on_cal1();
  }
}

I'm thinking that for my need, composition might be better that inheritance.

Thanks to everyone for the help.
#6

[eluser]dmorin[/eluser]
Honestly, if you know enough about OO to judge whether composition or inheritance is better suited for your application, then don't worry about the best way to do in in CI, just do it how you normally would. CI's implementation, while convenient, totally pollutes the local scopes of all object instances which can become a real pain. So yes, if you want to pass around an object, just do it how you would using PHP and don't worry about making sure it's also available at $this->object.
#7

[eluser]wowdezign[/eluser]
That is why I value the CodeIgniter community! dmorin, thanks for the clarification.
Quote:CI’s implementation, while convenient, totally pollutes the local scopes of all object instances which can become a real pain.

While I have read and studied many books on OOP and have played around with it, I have had a tough time using what I've learned in the CI environment because I didn't know enough about OOP to be confident in the conclusion I was drawing. Now that you've confirmed this, I can stop thinking 'well, I must have misunderstood that', or 'man, this discouraging'.

This lack of understanding (and/or experience) on my part, has made it challenging for me to apply some techniques that I thought should work but do not. Particularly, when I have tried to extend the framework.

For example, I am trying to create this Availability Calendar class that uses multiple instances of the Calendar class to show a 3 month view. Since CodeIgniter already has one, why create a new one?

But this "scoping" issue keeps throwing me.

So, if I was going to make a long story short -although it is too late for that- I am saying, "Thanks for letting me know that I am not crazy"! My inexperience coupled with my research wasn't reconciling and your direct comment helps.




Theme © iAndrew 2016 - Forum software by © MyBB