How to use the same object for multiple interceptors in CodeIgniter?

I have the following hook configuration (in hooks.php)

$hook['post_controller_constructor'][] = array(
    'class'     => 'MY_DataCollection',
    'function'  => 'init',
    'filename'  => 'MY_DataCollection.php',
    'filepath'  => 'hooks'
);

$hook['post_controller'][] = array(
    'class'     => 'MY_DataCollection',
    'function'  => 'post_controller',
    'filename'  => 'MY_DataCollection.php',
    'filepath'  => 'hooks'
);

What I want to do is create an instance of the class in post_controller_constructor, and then run the method init. Then for post_controllerrun post_controller, but using the same instance. CodeIgniter creates an instance of the class again.

Next, I tried something a little smart, I thought:

require_once APPPATH . 'hooks/MY_DataCollection.php';
$collection = new MY_DataCollection;

$hook['post_controller_constructor'][] = array(
    'function'  => array($collection, 'init'),
    'filename'  => 'MY_DataCollection.php',
    'filepath'  => 'hooks'
);

$hook['post_controller'][] = array(
    'function'  => array($collection, 'post_controller'),
    'filename'  => 'MY_DataCollection.php',
    'filepath'  => 'hooks'
);

This does not work, since the hook class in CI requires, and I get:

Fatal error: Cannot redeclare class MY_DataCollection in /var/www/application/hooks/MY_DataCollection.php on line 7

So, I got rid of the file path information:

require_once APPPATH . 'hooks/MY_DataCollection.php';
$collection = new MY_DataCollection;

$hook['post_controller_constructor'][] = array(
    'function'  => array($collection, 'init')
);

$hook['post_controller'][] = array(
    'function'  => array($collection, 'post_controller')
);

It does not even start, as in the class CI_Hooks, this check is performed in _run_hook:

if ( ! isset($data['filepath']) OR ! isset($data['filename']))
{
    return FALSE;
}

, , , , - .

+4
2

Hmmz, , - . , - :

class MyHooks 
{
    public static $object_you_need;

    public function init() 
    {
        $object = new MyObject();
        $object->init();
        self::$object_you_need = $object;
    }

    public function post_controller()
    {
        $object = self::$object_you_need;
        // do stuff with that object
    }
}

, , .

+4

CI3, CI2

  • MY_Hooks.php app/core directory
  • Hooks.php CI 3 MY_Hooks.php, MY_Hooks Extends CI_Hooks, system/core/hooks.php
  • call_hook _call_hook ( '_')
  • private _run_hook public
  • ,

,

class Hook_hook{
    private $init = 'function inits not called';

    public function inits(){
        $this->init = 'function inits called';
    }

    public function load(){
        echo '<script>alert("'.$this->init.'")</script>';
    }
}

config/hooks.php

$hook['post_controller_constructor'][] = array(
    'class'     => 'Hook_hook',
    'function'  => 'inits',
    'filename'  => 'Hook_hook.php',
    'filepath'  => 'hooks'
);

$hook['post_controller'][] = array(
    'class'     => 'Hook_hook',
    'function'  => 'load',
    'filename'  => 'Hook_hook.php',
    'filepath'  => 'hooks'
);
+3

All Articles