Skip to content

Instantly share code, notes, and snippets.

@alexburner
Created February 9, 2012 23:05
Show Gist options
  • Save alexburner/1784108 to your computer and use it in GitHub Desktop.
Save alexburner/1784108 to your computer and use it in GitHub Desktop.
__get __set PHP magic
class Document {
private $_text;
public function __get( $name ) {
echo 'Document __get( ' . $name . ' )<br />';
$method = 'get' . $name;
if ( !method_exists( $this, $method ) ) { return null; }
return $this->$method();
}
public function __set( $name, $value ) {
echo 'Document __set( ' . $name . ' )<br />';
$method = 'set' . $name;
if ( !method_exists( $this, $method ) ) { return null; }
$this->$method( $value );
}
function getText() {
return $this->_text;
}
function setText( $value ) {
$this->_text = $value;
}
}
$doc = new Document();
$doc->text = 'TEXT <br />';
echo $doc->text;
$doc->title = 'TITLE <br />';
echo $doc->title;
echo 'NOT BROKEN <br />';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment