Welcome Guest, Not a member yet? Register   Sign In
Utilizing CI Resources within Extended Core Class
#1

[eluser]wbremen[/eluser]
I extended the Core Class Lang (MY_lang) and would like to use CI Resources within it.

As the core classes and libraries are not automatically loaded in extended core classes, e.g. following method does not work:

Code:
echo $this->config->item('base_url');

I am used to this behaviour in extended or self created libraries and tried the approach which is used in those cases.

Code:
class MY_Lang extends CI_Lang {

    function __construct()
    {
        parent::__construct();
        $CI =& get_instance();
    }

    .....

Code:
echo $CI->config->item('base_url');

But this does not work either, somehow this brings the current controller to fail finding the CI_Controller class.

Does anyone know a workaround for this?
#2

[eluser]wbremen[/eluser]
I solved the problem myself.

For some reasons you can not define an Intence for the whole class, it does not work with $this-> either.

The solution I found is to use the get_instance() function directly when needed:

Code:
echo get_instance()->config->item('base_url');
#3

[eluser]Twisted1919[/eluser]
Or you can do it how everybody else is doing it:
Code:
class MY_Lang extends CI_Lang {
    
    private $ci=null;

    function __construct()
    {
        parent::__construct();
        $this->ci =& get_instance();
    }

    function foo()
    {
        echo $this->ci->config->item('base_url');
    }

    .....
#4

[eluser]wbremen[/eluser]
As I wrote, that somehow does not work, allthough my try is exactly what you wrote

[quote author="wbremen" date="1297106660"]For some reasons you can not define an Intence for the whole class, it does not work with $this-> either.[/quote]
#5

[eluser]Twisted1919[/eluser]
I do it and it works perfectly, the documentation states this is the way you need to do it and it needs to work like that, because, well it is logical to work this way because of the oop programming .

So if it doesn't work as suggested, you need to debug your code and see why is not working, because, it is a mistake into your end for sure.
#6

[eluser]wbremen[/eluser]
Since I am using the same method in my libraries I was sure it would work, too, but for the same reason, I know how to implement it the way it is ment to be and I can't find any mistakes I did - Even in a completly empty Class.

Additionally, also CI's native Core classes use get_instance() directly, and not via $this->

Code:
foreach (get_instance()->load->get_package_paths(TRUE) as $package_path)
system/core/Lang.php

Also, the Error I get is not directly related to the CI Instance - My controller simply can not find the CI_Controller class anymore.




Theme © iAndrew 2016 - Forum software by © MyBB