Welcome Guest, Not a member yet? Register   Sign In
How to extend a base class class library?
#1

[eluser]doors[/eluser]
I am writing a modules class that will be the base class for my other modules such a s payment and shipping.

I am writing a test to see if I can extend a library class but it's not working. Let me know where I am going wrong. The test class is as follows.

Code:
class Test{
          
            function __construct(){
               echo 'Test Base Class Controller!";
            }

}


class ExtendTest extends Test{

            function __construct(){
               parent::Controller();
            }
}


Within my controller class I load the ExtendTest class as follows:

Code:
class Welcome extends Controller{

            function __construct(){
               parent::Controller();
               $this->load->library('ExtendTest');
            }
}

I am getting an error saying:


Fatal error: Class 'Test' not found in ~\system\application\libraries\ExtendTest.php
#2

[eluser]xadio[/eluser]
Where is your ExtendTest.php located?
Is your application located in /system/application/?

There is a modules library that already exists. I haven't used it, but conceptually it seems good.
Matchbox: http://code.google.com/p/matchbox/
#3

[eluser]doors[/eluser]
Can someone please help me with this issue!
#4

[eluser]doors[/eluser]
Both files are located in the library folder.
#5

[eluser]xadio[/eluser]
According to your controller
Code:
class Welcome extends Controller{

            function __construct(){
               parent::Controller();
               $this->load->library('ExtendTest');
            }
}
you haven't loaded Test.php if class Test is not in ExtendTest.php.

You can either load the Test
Code:
class Welcome extends Controller{

            function __construct(){
               parent::Controller();
               $this->load->library('Test','ExtendTest');
            }
}
, but this will initialize Test.
Or you could require Test if it is an abstract class:
/ExtendTest.php
Code:
require_once(APPPATH.'/libraries/Test.php');
  class ExtendTest extends Test {
   //...
  }
#6

[eluser]doors[/eluser]
I did the require. I was wondering if seeing that the ExtendTest extends another Class if the loader would load it and let everything flow freely.

Thanks.
#7

[eluser]xadio[/eluser]
[strike]I have been working with Loader.php and there is nothing in Loader. This may be because
array class_parents( mixed $class [, bool $autoload ] )

is PHP5 specific. CI supports PHP4 so they aren't going to do that. The only thing close is
string get_parent_class( mixed $object )

You could extend the Loader.php:
/system/application/libraries/MY_Loader.php
Code:
class MY_Loader extends CI_Loader {
  function _ci_init_class($class, $prefix = '', $config = FALSE) {    
    // Is there an associated config file for this class?
    if ($config === NULL) {
      $config = NULL;
      if (file_exists(APPPATH.'config/'.$class.EXT)) {
        include(APPPATH.'config/'.$class.EXT);
      }
    }
        
    if ($prefix == '') {
      $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
    } else {
      $name = $prefix.$class;
    }
        
    // Set the variable name we will assign the class to
    $class = strtolower($class);            
    $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];

    // Require Parents
    foreach(class_parents($name, FALSE) as $parent) {
      //load the parents, but this will require a rewrite of _ci_load_class()
      $subclass = APPPATH.'libraries/'.config_item('subclass_prefix').$parent.EXT;
      $appfilepath = APPPATH.'libraries/'.$parent.EXT;
      $basefilepath = BASEPATH.'libraries/'.$parent.EXT;
      if(!in_array($subclass, $this->_ci_classes) &&
        (!in_array($appfilepath, $this->_ci_classes) ||
         !in_array($basefilepath, $this->_ci_classes)) {
        _ci_load_class($parent);
      }
    }

                
    // Instantiate the class        
    $CI =& get_instance();
    if ($config !== NULL) {
      $CI->$classvar = new $name($config);
    } else {        
      $CI->$classvar = new $name;
    }
  }
}

Haven't tested, but should work.[/strike]
#8

[eluser]xadio[/eluser]
So that didn't work. I am working on something like this and will post on my results later.




Theme © iAndrew 2016 - Forum software by © MyBB