IngmarBoddington
6/1/2014 - 3:56 PM

Verbose - toString and get / set enforcement

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;
    }
}