Skip to content

Instantly share code, notes, and snippets.

@jsjohnst
Created December 29, 2009 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsjohnst/265703 to your computer and use it in GitHub Desktop.
Save jsjohnst/265703 to your computer and use it in GitHub Desktop.
<?php
abstract class write_once_class
{
function __get($key) {
if(property_exists(get_class($this), $key)) {
return $this->$key;
} else {
throw new Exception("Can't get a variable which isn't declared.");
}
}
function __set($key, $value) {
if(property_exists(get_class($this), $key) && is_null($this->$key)) {
$this->$key = $value;
} else {
throw new Exception("Can't set a variable which has been previously set or isn't declared.");
}
}
}
class my_class extends write_once_class
{
protected $foo = null;
protected $bar = null;
protected $baz = null;
}
$c = new my_class();
$c->foo = 33;
print $c->foo;
$c->foo = 39;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment