One man’s voice Thoughts, rants and commentary from a husband, father of five and professional web geek

9Aug/070

Class for getting variable information

This is a work in progress. I hope to eventually have it set to where it will output a nice pretty dump of data that is also useful to the developer. Anyway, it is usable as is, but not as useful as could be.

<?php
/**
 * Class that inspects a variable
 */
class Variable_Data_Dump
{
    /**
     * The name of the variable passed for inspection
     * 
     * @access protected
     * @var string
     */
    protected $varName;
    
    /**
     * The string to send back when the variable name is not found
     * 
     * @access protected
     * @var string
     */
    protected $varNameNotFound = '[name_not_found]';
    
    /**
     * The type of the variable passed for inspection
     * 
     * @access protected
     * @var string
     */
    protected $varType;
    
    /**
     * The data in the variable passed for inspection
     * 
     * @access protected
     * @var mixed
     */
    protected $varContent;
    
    /**
     * The output method to be used (CLI or HTML)
     * 
     * @access protected
     * @var string
     */
    protected $outputMode;
    
    
    /**
     * Object constructor - sets object properties
     * 
     * @access public
     * @param mixed $var A reference to the varible to be inspected
     * @param boolean $dumpNow Whether to dump the variable information on instantiation
     */
    public function __construct(&$var, $dumpNow = false) { 
        $this->setVarName($var);
        $this->setVarType($var);
        $this->setVarContent($var);
        $this->setOutputMode();
        
        // Output is the app wants it right away
        if ($dumpNow) {
            $this->dumpInfo();
        }
    }
    
    /**
     * Gets the name of the variable to be inspected
     * 
     * @access public
     * @param mixed $var A reference to a variable that is to be inspected
     * @return string The string name of the variable, or '[name_not_found]' if not found
     */
    public function setVarName(&$var) {
        // Before we start, we need to read the data for the var we are checking
        // into a temporary var since we need to pass a reference of the var we
        // want the name for. Without this, we would actually be working with 
        // the original variable, screwing things up royally.
        $passed = $var;
        
        // Initialize the variable name return variable
        $varName = '';
        
        // Now the fun part... we change the value of the variable whose name we 
        // want to something random (not the number 4 onion)
        $var = 'Scarlett_'.rand().'_Johannson';
        
        // Now we loop through the GLOBAL array and see if we find the GLOBAL array 
        // key that matches this value
        foreach ($GLOBALS as $k => $v) {
            // If the value of the GLOBAL is the same as the temp value...
            if ($v === $var) {
                // Then we snag the GLOBAL key and use it as our var name
                $varName = $k;
                
                // And since we have it, we no longer need to loop
                break;
            }
        }
        
        // If there was no name found inform the app
        if (empty($varName)) {
            $varName = $this->varNameNotFound;
        }
        
        // Now change the value of the variable back to its original state
        $var = $passed;        
        
        // And finally set the name
        $this->varName = $varName;
    }
    
    /**
     * Sets the type for this variable
     * 
     * @access protected
     * @param mixed $var The variable to be inspected
     */
    protected function setVarType($var) {
        $this->varType = gettype($var);
    }
    
    /**
     * Sets the data value for this variable
     * 
     * @access protected
     * @param mixed $var The variable to be inspected
     */
    protected function setVarContent($var) {
        $this->varContent = $var;
    }
    
    /**
     * Actually dumps the information about this variable in a somewhat 
     * reasonably formatted method
     * 
     * @access public
     * @param boolean $echo Flag to determine whether to immediately output the data
     * @return string|void Will either echo the data or return a string representation of it
     */
    public function dumpInfo($echo = true) {
        // Prepare the output string
        $output = '';
        
        // If we have a var name, add it to the output
        if ($this->varName !== $this->varNameNotFound) {
            $output .= 'Variable passed: $' . $this->varName . "\n";
        }
        
        // Make some nicer formatted output
        $output .= 'Variable type: ' . $this->varType . "\nVariable Value: " . print_r($this->varContent, true);
        
        // Prepare the output for outputMode
        $output = $this->prepareOutput($output);
        
        // This might not be necessary, or it and other stuff may be
        //$this->varContent = null;
        
        // Do we echo or return?
        if ($echo) {
            echo $output;            
            return;
        } else {
            return $output;
        }
    }
    
    /**
     * Set the output mode for the correct PHP environment
     * 
     * @access protected
     */
    protected function setOutputMode() {
        $this->outputMode = PHP_SAPI == 'cli' ? 'text' : 'html';
    }
    
    /**
     * Prepare the output based on outputMode
     * 
     * @access protected
     * @param string $output The output string to output
     * @return string A prepared output string
     */
    private function prepareOutput($output) {
        if ($this->outputMode == 'html') {
            $output = '<pre>' . $output  . "</pre>\n";
        }
        
        return $output;
    }
}
?>

 

Usage:

<?php
$arrayTest = array('Penny' => 1, 'Nickel' => 5, 'Dime' => 10, 'Quarter' => 25);
$integerTest = 344345;
$floatTest = 65.0998;
$arrayDump = new Variable_Data_Dump($arrayTest, true);
$intDump = new Variable_Data_Dump($integerTest);
$intDump->dumpInfo();
$objDump = new Variable_Data_Dump($arrayDump, true);
?>

 

Sample output:

Variable passed: $arrayTest
Variable type: array
Variable Value: Array
(
    [Penny] => 1
    [Nickel] => 5
    [Dime] => 10
    [Quarter] => 25
)
Variable passed: $integerTest
Variable type: integer
Variable Value: 344345
Variable passed: $arrayDump
Variable type: object
Variable Value: Variable_Data_Dump Object
(
    [varName:protected] => arrayTest
    [varNameNotFound:protected] => [name_not_found]
    [varType:protected] => array
    [varContent:protected] => Array
        (
            [Penny] => 1
            [Nickel] => 5
            [Dime] => 10
            [Quarter] => 25
        )

    [outputMode:protected] => html
)
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.