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.