Welcome Guest, Not a member yet? Register   Sign In
Custom library extending custom libraries
#1

[eluser]Spockz[/eluser]
I'm trying to get my custom libraries to extend eachother. However if I extend a custom classfile I get a 'class not found' error in the file 'my_library.php'. I can resolve this by including a 'include' command. However this is not a nice solution, as I'd rather use the autoloader.

I've got the following code:
Code:
class My_Controller extends Controller{
  function __construct() {
    $this->load->library('my_library');
  }
}

Now i've the file 'my_library.php'. Placed in the application/libraries directory.
Code:
class My_Library extends My_Parentlibrary {
  ...
}

The file 'my_parentlibrary.php' contains the class:
Code:
class My_Parentlibrary {
  ...
}

Is there anyone familiar with this problem? Or knows a solution to this?
#2

[eluser]wiredesignz[/eluser]
include is the best (proper) method, autoload will instantiate your parent class, load_class() is also available but needs to be used with care.

The MY_ prefix should be reserved for CI core extensions.
#3

[eluser]Spockz[/eluser]
But then you would have to maintain your include directives. That could be a lot of work. Sad
#4

[eluser]xwero[/eluser]
I think there is no other way than to use an include in your my_library.php file. There is no magic way to include the my_parentlibrary.php file.
#5

[eluser]xwero[/eluser]
You can use APPPATH to create 'relative' directories.
#6

[eluser]Spockz[/eluser]
On second thought. As CI doesn't use the __autoload() function. I'll utilize it myself. Smile

http://nl2.php.net/__autoload
#7

[eluser]wiredesignz[/eluser]
And with PHP5 __autoload() you will still be using include or require.

xwero provided the solution to your directive question.
#8

[eluser]Spockz[/eluser]
[quote author="wiredesignz" date="1206984079"]And with PHP5 __autoload() you will still be using include or require.

xwero provided the solution to your directive question.[/quote]

Indeed, but I won't have to write them manually myself.
#9

[eluser]patbert[/eluser]
Is there no other solution to this?

I have three libraries, each need to share a few functions and variables. I want to have a library 'Shared.php' and then have the other libraries extend it. Like the original poster said, this fails with a class not found error.
#10

[eluser]Spockz[/eluser]
I've checked some things since then.

Make sure the filenames are in the correct case. So the same as your class names. Furthermore CI likes it if you use an uppercase letter for the first letter.

Or else you could always use this code (I'd modify it if I were you, but the basics are the same):
Code:
function __autoload($aClass) {
  $file = buildAppPath($aClass);
  if(file_exists($file)) {
    require_once $file;
  }
}

I load this with an helper. :-)




Theme © iAndrew 2016 - Forum software by © MyBB