• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get class reference

#1
[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?

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

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

        self::$instance =& $this;

        return self::$instance;
    }
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.