0

我对 PHP 中的整个 OOP 范式相当陌生,但到目前为止我真的很喜欢它。我目前正在编写一个 EventSender 类,它应该收集一些信息,然后将事件触发到 EventHandler 以及将事件写入事件日志。

当我来到“触发”部分时,让我感到震惊的是,我真的很想有一个简单的解决方案来验证我声明的所有变量都已设置。有没有一种简单的方法可以做到这一点,或者甚至是 PHP 中的内置函数?

此外,下面粘贴的代码是迄今为止我的课程的实际代码,所以如果您有任何其他意见,请随时阐述您的想法:-)

class Event extends Base {

private $eventKey;
private $userID;
private $value;

private function __construct($eventKey){

    $sql = Dbc::getInstance();

    //Set and escape the EVENT_KEY.
    $this->eventKey = $sql->real_escape_string($eventKey);

    $sql->select_db('my_event_db');
    $result = $sql->query('SELECT idx FROM event_types WHERE event_key = $this->$eventKey');

    //Verify that the event key given is correct and usable.
    //If failed throw exception and die.
    if($result->num_rows != 1){

        $err = 'ERROR: Illegal EVENT_KEY sent.';
        throw new Exception($err);

    }

}

public function setUserID($userID) {

    $this->userID = $userID;
    if(is_numeric($this->userID) != TRUE){

        $err = 'ERROR: Value passed as userID is not numeric.';
        throw new Exception($err);

    }

}

public function setValue($value) {

    $this->value = $value;
    //The isJson function comes from a Trait that my Base class uses.
    //The method will also throw a exception if it doesn't pass the test.
    self::isJson($this->value);

}

public function fire () {

    /* Here I want some code that swiftly checks if all declared vars have been set, which makes my method "ready to fire".. */        

}

最好的问候,安德烈 V。

4

3 回答 3

1

根据 Lajos Veres 的回答,我设法构建了一个可以添加到 Trait 的类(在这种情况下我做了什么),并且完全按照我在最初的问题中写的那样做。我只是想分享它,如果有人想重用它:-)

protected function allPropertiesSet($self){

    /*
     * This class is dependent on the ReflectionClass.
     * This class can be called with self::allPropertiesSet(get_class());
     * The class can be called in any class who uses this trait, even if it is inherited. It's function is to validate if all your defined variables are set.
     * It will return true if all variables are set, otherwise it will return false. 
     * Some credit goes to Lajos Veres from Stackoverflow for pointing me in the right direction.
     * Author: André Valentin
     * Created: 30-10-2013
     */

    $class = new $self;
    $reflect = new ReflectionClass($class);

    $props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_STATIC);
    $prop_array = array();

    foreach($props AS $prop){

        $var_name = $prop->getName();
        $class_name = $prop->class;

        if($class_name == $self){

            $prop_array[] = $var_name;

        }

    }

    foreach($prop_array AS $value){

        $var_name = $value;

        if(!isset($this->$var_name)){

            return false;

        }

    }

    return true;

}
于 2013-10-30T16:51:33.567 回答
0

为了理智起见,请在调用之前明确定义管理实例状态的规则fire(),并将其移至单独的函数。所以你的fire()变成

function fire() {
    if ($this->validate_state()) {
        /// do whatever
        ...
    } else {
       /// report issues
    }
}

您的验证器只需检查所有需要就地的东西,即

function validate_state() {
    if ( isset($this->some_property) )
     ....
}

或者,如果您的状态检查只是想确保设置了默认值,请确保在您的__construct构造函数中完成此操作。这样您就知道合理期望定义哪些值。

于 2013-10-28T22:37:49.127 回答
0

使用Reflection您可以列出类的属性

http://www.php.net/manual/en/reflectionclass.getproperties.php

但我觉得这有点矫枉过正...

于 2013-10-28T22:32:51.220 回答