Created
June 1, 2014 15:56
-
-
Save IngmarBoddington/23fa9f355cd4e3c016f3 to your computer and use it in GitHub Desktop.
Verbose - toString and get / set enforcement
This file contains hidden or 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 | |
class templateClass { | |
private $variable = 'pretest'; //Test variable | |
function __contruct() { | |
//Init | |
} | |
function __destruct() { | |
//Close | |
} | |
function __sleep() { | |
//Variables to serialize | |
} | |
function __wakeup() { | |
//Re-init | |
} | |
function __toString() { | |
$toString = 'Object dump:<br />'; | |
$toString .= 'Class = '.get_class($this).'<br />'; | |
$toString .= '=====================================<br />'; | |
$allVariables = get_object_vars($this); | |
$toString .= 'Object Properties ('.count($allVariables).' total):<br />'; | |
foreach($allVariables as $key => $value) { | |
$toString .= "\t".$key.' => '.$value.'<br />'; | |
} | |
$toString .= '=====================================<br />'; | |
$allMethods = get_class_methods(get_class($this)); | |
$toString .= 'Object / Class Methods ('.count($allMethods).' total):<br />'; | |
foreach ($allMethods as $key => $value) { | |
$toString .= "\t".$key.' => '.$value.'<br />'; | |
} | |
return $toString; | |
} | |
function __isset($var) { | |
} | |
function __unset($var) { | |
} | |
function __get($var) { | |
$funcName = 'get'.ucFirst($var); | |
if (method_exists($this, $funcName)) { | |
return call_user_func(array($this, $funcName)); | |
} else { | |
throw new Exception("Attempted to get unregistered variable ($var)"); | |
} | |
} | |
function __set($var, $value) { | |
$funcName = 'set'.ucfirst($var); | |
if (method_exists($this, $funcName)) { | |
call_user_func(array($this, $funcName), $value); | |
} else { | |
throw new Exception("Attempted to set unregistered variable ($var = $value)"); | |
} | |
} | |
function setVariable($value) { | |
$this->variable = $value; | |
} | |
function getVariable() { | |
return $this->variable; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thx.
There's a missing "s" in __contruct, should be __construct