« New Server Alias | Disk space »
PHP Get and Set properties
By Lennard | April 11, 2006
Warning: preg_match() [function.preg-match]: Compilation failed: unrecognized character after (?< at offset 3 in /home/bakkerl/domains/lannerd.nl/public_html/wordpress/wp-content/plugins/codesnippet/lib/geshi.php on line 2132
Warning: preg_match() [function.preg-match]: Compilation failed: unrecognized character after (?< at offset 3 in /home/bakkerl/domains/lannerd.nl/public_html/wordpress/wp-content/plugins/codesnippet/lib/geshi.php on line 2132
I stole this piece of code. But placed it here so i find it back again.
-
<?php
-
/*
-
* Class BaseClass
-
*
-
*
-
*
-
*
-
* $objTest = new BaseClass();
-
* print $objTest->data . "n";
-
*
-
* $objTest->data = "bar"; //Works.
-
* print $objTest->data;
-
*
-
* $objTest->id = 5; //Error: Property is read-only.
-
*
-
*/
-
class BaseClass
-
{
-
-
// EXAMPLE PROPERTIES
-
"type" => "int",
-
"readonly" => true),
-
"type" => "string",
-
"readonly" => true),
-
"type" => "string",
-
"readonly" => false)
-
);
-
// END EXAMPLE PROPERTIES
-
-
/*
-
*
-
*/
-
private function __get($strProperty)
-
{
-
//Get a property:
-
return $this->properties[$strProperty]["value"];
-
else
-
{
-
throw new Exception("Property not defined");
-
return false;
-
}
-
}
-
-
/*
-
*
-
*/
-
private function __set($strProperty, $varValue)
-
{
-
//Set a property to a value:
-
{
-
//Check if property is read-only:
-
if ($this->properties[$strProperty]["readonly"])
-
{
-
throw new Exception("Property is read-only");
-
return false;
-
}
-
else
-
{
-
$this->properties[$strProperty]["value"] = $varValue;
-
return true;
-
}
-
}
-
else
-
{
-
throw new Exception("Property not defined");
-
return false;
-
}
-
}
-
-
/*
-
*
-
*/
-
private function __isset($strProperty)
-
{
-
//Determine if property is set:
-
}
-
-
/*
-
*
-
*/
-
private function __unset($strProperty)
-
{
-
//Unset (remove) a property:
-
}
-
-
}
-
?>
Topics: php | No Comments »