Post by [dmsuperman] on Apr 14, 2006 1:02:05 GMT
class enum{
public $vars;
public $value;
public $strict;
public $error;
function __construct($allowed, $strict=false){
if(!is_array($allowed)){
$allowed = array($allowed);
}
$this->vars = $allowed;
$this->strict = is_bool($strict) ? $strict : false;
}
function set($varvalue){
if(in_array($varvalue, $this->vars, $this->strict)){
$this->error = "";
$this->value = $varvalue;
return(true);
}else{
$this->error = "Invalid data tried";
$this->error = $this->strict ? $this->error . "\nStrict variable testing is on, ensure values are the same type" : $this->error;
return(false);
}
}
function get(){
return($this->value);
}
function debug(){
print("<pre>$" . "vars ->\n");
print_r($this->vars);
print("\n\n$" . "value ->\n");
nice_print($this->value);
print("\n\n$" . "strict ->\n");
nice_print($this->strict);
print("\n\n$" . "error ->\n" . $this->error . "</pre>");
}
}
//This just includes my nice_print function, check the Open Source board for it
include("nice_print.php");
$test = new Enum(array(1, "2", "3"), true);
$test->set(1);
print($test->get() . "<br />");
$test->debug();
print("<br /><br />");
$test->set(2);
print($test->get() . "<br />");
$test->debug();
This is like Eric's enum, except PHP. You initiate it like $test = new Enum(<array_of_valid_values>, <strict>);
Then set values like $test->set(<value>); If it's strict, it will test if it's in the array of valid values with the same type, if not it'll just see if it's the same value (integer and string independant). $test->get(); Returns the value of the variable, $test->debug(); will output data about the current value and error. It requires my nice_print function to be included in the page, or you can remove those lines if you like. $test->error; is the error message it contains for the last set variable, or empty if the last set variable was successful.
As usual, enjoy and do what you want with it.
public $vars;
public $value;
public $strict;
public $error;
function __construct($allowed, $strict=false){
if(!is_array($allowed)){
$allowed = array($allowed);
}
$this->vars = $allowed;
$this->strict = is_bool($strict) ? $strict : false;
}
function set($varvalue){
if(in_array($varvalue, $this->vars, $this->strict)){
$this->error = "";
$this->value = $varvalue;
return(true);
}else{
$this->error = "Invalid data tried";
$this->error = $this->strict ? $this->error . "\nStrict variable testing is on, ensure values are the same type" : $this->error;
return(false);
}
}
function get(){
return($this->value);
}
function debug(){
print("<pre>$" . "vars ->\n");
print_r($this->vars);
print("\n\n$" . "value ->\n");
nice_print($this->value);
print("\n\n$" . "strict ->\n");
nice_print($this->strict);
print("\n\n$" . "error ->\n" . $this->error . "</pre>");
}
}
//This just includes my nice_print function, check the Open Source board for it
include("nice_print.php");
$test = new Enum(array(1, "2", "3"), true);
$test->set(1);
print($test->get() . "<br />");
$test->debug();
print("<br /><br />");
$test->set(2);
print($test->get() . "<br />");
$test->debug();
This is like Eric's enum, except PHP. You initiate it like $test = new Enum(<array_of_valid_values>, <strict>);
Then set values like $test->set(<value>); If it's strict, it will test if it's in the array of valid values with the same type, if not it'll just see if it's the same value (integer and string independant). $test->get(); Returns the value of the variable, $test->debug(); will output data about the current value and error. It requires my nice_print function to be included in the page, or you can remove those lines if you like. $test->error; is the error message it contains for the last set variable, or empty if the last set variable was successful.
As usual, enjoy and do what you want with it.