简体   繁体   English

扩展Php类,扩展另一个类

[英]Extending a Php class which extends another class

I have a requirement where a process can be implemented in 2 different situation. 我有一个可以在2种不同情况下实施流程的要求。 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. 当前,我们使用值对象,在其中使用Zend Validate对象提交的每个字段上执行一系列验证项目。

The validation extends a base class eg 验证扩展了基类,例如

class ValueObject_test1 extends filter() ValueObject_test1类扩展了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: ValueObject_test1由以下组成:

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() 我想要实现的是ValueObject_test1的扩展,以便我的第二种情况将有一个附加的验证项以及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? 我的印象是,通过在_setData()中设置两个变量,是否应该对ValueObject_test1中的项目和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. 我的代码没有正确调用_setData,因为在父代 $:- 的$ this-> setData(testVar): _construct将调用该函数的$ this-> _ setData(testVar)版本。

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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