php - Dynamically build a class member variable -
i trying setup php (zend framework) project through injection dependence
. far when instanciated model, pass table
, view
(database view) model that:
$table = new my_table(); $view = new my_view(); $model = new my_model($table, $view);
all models extends same class take care of construction, message handling , getters forms interact model.
now have inject model
model
, looking passive static way of doing this. in model's parent class have added static method inject
called in application bootstrap. pass 2 string in form of key => value
key name of variable have created in model , value string represents class instanciated.
my_model::inject('dependentmodel', 'my_other_model')
the issue arise when try use key new member variable through following code :
protected function _initdependency() { $this->_table = null; foreach (self::$_staticdependency $key => $dependency) { $varname = '_' . $key; $this->{$$varname} = new $dependency(); } }
i following message
notice: undefined variable: _dependentmodel
what best way achieve that, knowing want create models ignorants of dependencies?
use arrays
class foo { private $_data = array(); protected function _initdependency() { $this->_table = null; foreach (self::$_staticdependency $key => $dependency) { $varname = '_' . $key; $this->_data[$varname] = new $dependency(); } } }
(as side effect removes variable-variables)
you can use __get()
, __set()
, __isset()
, __unset()
simulate property-behaviour.
Comments
Post a Comment