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;
}
, , , , - .