简体   繁体   中英

Extending a Php class which extends another class

I have a requirement where a process can be implemented in 2 different situation. One situation would be where the start date cannot be in the past and the other would be where it can.

Currently we utilise Value Objects where we perform a series of a validation items on each field submitted using Zend Validate objects.

The validation extends a base class eg

class ValueObject_test1 extends filter()

Filter is made up 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 is made up 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 to achieve is an extension of ValueObject_test1 so that my second situation will have an additional validation item as well as the items in ValueObject_test1()

I've written the following for my 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 is that the output from this only appears to validate my second situation validation and nothing on the second. I'm under the impression that by setting both variables in _setData() the validation should occur for items in ValueObject_test1 and the items in my ValueObject_test2?

class ValueObject_test2 extends ValueObject_test1 { 
    public function __construct($testVar, $testVar2) {
        $this->_setData($testVar, $testVar2);       
        parent::__construct($testVar);
    }   
    // _setFilters is identical to the 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 the _setData correctly because the $this-> setData(testVar) inside the parent:: _construct's will call the $this->_setData(testVar) version of the function.

If you want to override a function, your override will likely want to also run the logic of the parent.

<?php
class Foo {
    protected function _setFoobar() {       
        // Foo logic
    }
}

class Bar extends Foo {
    protected function _setFoobar() {       
        // custom Bar logic specific to the child class (Bar)
        parent::_setFoobar();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM