<pre>
<?
require_once('Property.php');
class PropertyTest extends Property
{
public function __construct() {
$this->addProperty('readonly', 'READ ONLY PROPERTY', true);
$this->addProperty('onlyNumbers', '0', false, 'getNumber', 'setNumber');
}
}
function getNumber( $value ) {
if ($value>0) {
return "+".$value;
}
return $value;
}
function setNumber( $value ) {
if (is_numeric($value)) {
// try to cast value to number
return 0 + $value;
}
throw new Exception("\"$value\" is not a number");
}
$p = new PropertyTest();
$p->test = 'VariableValue';
echo '$p->test = '. $p->test;
try {
$p->onlyNumbers = 15; // ok
$p->onlyNumbers = '15'; // ok
$p->onlyNumbers = 'e15'; // not ok
} catch( Exception $e ) {
echo '<BR><strong>ERROR</strong>: ';
echo '<BR>'.$e->getMessage(); // exception's message
}
echo "<BR>";
echo '$p->onlyNumbers = '. $p->onlyNumbers;
echo "<BR><strong>PropertyTest Object:</strong><br />";
var_dump( 'PropertyTest: ', $p);
?>
</pre>
|