简体   繁体   中英

How to check if all declared variables are set?

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.

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;

}

Using Reflection you can list the properties of a class

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

But I think this is overkill...

For sanity's sake, clearly define the rules governing the state of your instance before you call fire() , and move that to a separate function. So your fire() becomes

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

Your validator simply checks everything that needs to be in-place, ie

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

Alternatively, if your state checking just wants to ensure that default values are set, make sure this is done in your __construct constructor. That way you know which values are reasonably expected to defined.

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