Extending a Php class which extends another class -
i have requirement process can implemented in 2 different situation. 1 situation start date cannot in past , other can.
currently utilise value objects perform series of validation items on each field submitted using zend validate objects.
the validation extends base class e.g.
class valueobject_test1 extends filter()
filter made of: -
class filter { protected $_filter; protected $_filterrules = array(); protected $_validatorrules = array(); protected $_data = array(); protected $_objdata = array(); protected $_options = array(zend_filter_input::escape_filter => 'striptags'); protected $_runvalidation = true; protected function _setfilter() protected function _addfilter() protected function _addvalidator() protected function _adddata() protected function _addobject() protected function _addoption() public function getdata() public function haserrors() public function getmessages() public function getvalidationstate() public function __get() public function __isset() public function __set() }
valueobject_test1 made of:
class valueobject_test1 extends filter { public function __construct($testvar) { $this->_setdata(testvar); $this->_setfilters(); $this->_setvalidators(); if($this->_runvalidation) { $this->_setfilter(); } } protected function _setfilters(){ $this->_addfilter("*", "stringtrim"); } protected function _setdata($testvar) { $this->_adddata('testvar', $testvar); } protected function _setvalidators() { $this->_addvalidator('testvar', array(new zend_validate(), 'presence'=>'required', 'messages'=>'enter something')); } }
what i'm trying achieve extension of valueobject_test1 second situation have additional validation item items in valueobject_test1()
i've written following second situation:-
<?php class valueobject_test2 extends valueobject_test1 { public function __construct($testvar, $testvar2) { $this->_setdata($testvar, $testvar2); $this->_setfilters(); $this->_setvalidators(); if($this->_runvalidation) { $this->_setfilter(); } } protected function _setfilters(){ $this->_addfilter("*", "stringtrim"); } protected function _setdata($testvar, $testvar2) { $this->_adddata('testvar', $testvar); $this->_adddata('testvar2', $testvar2); } protected function _setvalidators() { $this->_addvalidator('testvar2', array(new zend_validate(), 'presence'=>'required', 'messages'=>'enter something')); } }
the issue i'm having output appears validate second situation validation , nothing on second. i'm under impression setting both variables in _setdata() validation should occur items in valueobject_test1 , items in valueobject_test2?
class valueobject_test2 extends valueobject_test1 { public function __construct($testvar, $testvar2) { $this->_setdata($testvar, $testvar2); parent::__construct($testvar); } // _setfilters identical parent implementation protected function _setdata($testvar, $testvar2) { $this->_adddata('testvar2', $testvar2); parent::_setdata($testvar); } protected function _setvalidators() { $this->_addvalidator('testvar2', array(new zend_validate(), 'presence'=>'required', 'messages'=>'enter something')); parent::_setvalidators(); } }
my code doesn't call _setdata correctly because $this->setdata(testvar) inside parent::_construct's call $this->_setdata(testvar) version of function.
if want override function, override want also run logic of parent.
<?php class foo { protected function _setfoobar() { // foo logic } } class bar extends foo { protected function _setfoobar() { // custom bar logic specific child class (bar) parent::_setfoobar(); } }
Comments
Post a Comment