oop - cakephp override construct in child controller -
i'm wondering if possible inherit/override constructors in kid controllers in cakephp.
in appcontroller.php
i have this:
public function __construct( $request = null, $response = null ) { parent::__construct( $request, $response ); $this->email = new cakeemail(); $this->_constants = array(); $this->_constants['some_var'] = $this->model->find( 'list', array( 'fields' => array( 'model.name', 'model.id' ) ) ); }
and in kid controller somecontroller.php, inherits parent constructor
public function __construct( $request = null, $response = null ) { parent::__construct( $request, $response ); }
and when tried access $this->email , $this->_constants['some_var'] both null. set code straight in somecontroller.php instead of inheriting, worked.
did wrong or not approachable cake? tried same function beforefilter(), same thing happened. makes sense each controller have own beforefilter().
i wouldn't seek overriding _construct' function of appcontroller. that's the
beforefilter,
beforerender` methods for. looks trying pass vars each controller appcontroller. can so...
class appcontroller extends controller { var $_constants = array(); public function beforefilter(){ $this->_constants[] = array('this', 'that', 'the other'); } }
and in model's controller can access variable so...
class userscontroller extends appcontroller { public function add(){ pr($this->_constants); } }
it's different story if trying send variables view (slightly). utilize set method
class appcontroller extends controller { public function beforefilter(){ $this->set('_constants', array('this', 'that', 'the other')); } }
and in view can phone call _constants
variable pr($_constants);
. because in appcontroller should available on every view.
oop class cakephp inheritance constructor
No comments:
Post a Comment