Created
December 29, 2009 23:32
-
-
Save jsjohnst/265703 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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