CodeIgniter Forums
How to get class reference - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to get class reference (/showthread.php?tid=2078)



How to get class reference - El Forum - 07-15-2007

[eluser]mezoni[/eluser]
I want have a reference a class in class constructor.
By example.

Code:
class Test extends Controller
{
    function Test()
    {
        parent::Controller();

        virtual_export('page_get_title', &$this, '_page_get_title');
    }

    function _page_get_title()
    {
        return $this->title;
    }
}

class Hooker  // library
{
    function hook_post_controller()
    {
        virtual_call('page_render');
        // ..
    }
}

class Page  // library
{

    function Page()
    {
        virtual_export('page_render', &$this, 'page_render');
    }

    function page_render()
    {
        $title = virtual_call('page_get_title');
        // ...
    }
}

But CI create object like this.

Code:
$CI = new $class();
$CI->$classvar = new $name;

If I use reference in class constructor I have copy of object but not reference.

if this were so...

Code:
$CI =& new $class();
$CI->$classvar =& new $name;

...then this will be possible.

But I don't know how to get class reference from class constructor.

I there any way to do this?


How to get class reference - El Forum - 07-15-2007

[eluser]Glen Swinfield[/eluser]
Assuming php5:

Code:
class Test extends Controller
{
    function Test()
    {
        parent::Controller();

        self::$instance =& $this;

        return self::$instance;
    }
}