duanjian4698 2013-10-28 22:06
浏览 38
已采纳

如何检查是否设置了所有声明的变量?

I am rather new to the whole OOP paradigm in PHP, but I'm really loving it so far. I am currently writing a EventSender class, which should gather some information, and then fire the event to a EventHandler as well as writing the event to a eventlog.

When I came to the "firing" part, it struck me that I really would love a simple solution to validating that all my declared variables had been set. Is there an easy way to do so, or maybe even a in-built function in PHP?

Also, the code pasted below is the actual code for my class so far, so if you have any other remarks feel free to elaborate your thought :-)

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".. */        

}

Best regards, André V.

  • 写回答

3条回答 默认 最新

  • douhuanglou1445 2013-10-30 16:51
    关注

    Based on Lajos Veres answer, I managed to build a class which can be added to a Trait (what is what I did in this case) and did exactly what I wrote in my initial question. I just wanted to share it, if anyone wanted to reuse it :-)

    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;
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)