PHP Classes
elePHPant
Icontem

File: PropertyTest.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Lucas Ara˙jo  >  Property  >  PropertyTest.php  >  Download  
File: PropertyTest.php
Role: Example script
Content type: text/plain
Description: Property class test
Class: Property
Add read-only class variables dynamically
Author: By
Last change:
Date: 8 years ago
Size: 1,050 bytes
 

Contents

Class file image Download
<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>