Created
February 9, 2012 23:05
-
-
Save alexburner/1784108 to your computer and use it in GitHub Desktop.
__get __set PHP magic
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
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