Skip to content

Instantly share code, notes, and snippets.

@IngmarBoddington
Created June 1, 2014 15:56
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 IngmarBoddington/23fa9f355cd4e3c016f3 to your computer and use it in GitHub Desktop.
Save IngmarBoddington/23fa9f355cd4e3c016f3 to your computer and use it in GitHub Desktop.
Verbose - toString and get / set enforcement
<?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;
}
}
@shayja
Copy link

shayja commented Jul 11, 2015

Thx.
There's a missing "s" in __contruct, should be __construct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment