Changeset 3532

Show
Ignore:
Timestamp:
10/05/2008 10:45:22 AM (2 months ago)
Author:
Shadowhand
Message:

Fixing recursion in Kohana::debug_var(), fixes #833. This still does not help with displaying private/protected properties, so the bug will remain open.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/classes/kohana.php

    r3529 r3532  
    14961496         * any variable. 
    14971497         * 
    1498          * @param   mixed   variable to dump 
     1498         * @param   mixed    variable to dump 
     1499         * @param   boolean  internal recursion 
    14991500         * @return  string 
    15001501         */ 
    1501         public static function debug_var($var) 
    1502         { 
     1502        public static function debug_var($var, $recursion = FALSE) 
     1503        { 
     1504                static $objects; 
     1505 
     1506                if ($recursion === FALSE) 
     1507                { 
     1508                        $objects = array(); 
     1509                } 
     1510 
    15031511                switch (gettype($var)) 
    15041512                { 
    15051513                        case 'object': 
     1514                                // Unique hash of the object 
     1515                                $hash = spl_object_hash($var); 
     1516 
    15061517                                $object = new ReflectionObject($var); 
    15071518                                $more = FALSE; 
    15081519                                $out = 'object '.$object->getName().' { '; 
    1509                                 foreach ($object->getProperties() as $property) 
    1510                                 { 
    1511                                         if ($property->isPublic()) 
     1520 
     1521                                if ($recursion === TRUE AND in_array($hash, $objects)) 
     1522                                { 
     1523                                        $out .= '*RECURSION*'; 
     1524                                } 
     1525                                else 
     1526                                { 
     1527                                        // Add the hash to the objects, to detect later recursion 
     1528                                        $objects[] = $hash; 
     1529 
     1530                                        foreach ($object->getProperties() as $property) 
    15121531                                        { 
    1513                                                 $out .= ($more === TRUE ? ', ' : '').$property->getName().' => '.self::debug_var($property->getValue($var)); 
     1532                                                $out .= ($more === TRUE ? ', ' : '').$property->getName().' => '; 
     1533                                                if ($property->isPublic()) 
     1534                                                { 
     1535                                                        $out .= self::debug_var($property->getValue($var), TRUE); 
     1536                                                } 
     1537                                                elseif ($property->isPrivate()) 
     1538                                                { 
     1539                                                        $out .= '*PRIVATE*'; 
     1540                                                } 
     1541                                                else 
     1542                                                { 
     1543                                                        $out .= '*PROTECTED*'; 
     1544                                                } 
    15141545                                                $more = TRUE; 
    15151546                                        } 
     
    15231554                                        if ( ! is_int($key)) 
    15241555                                        { 
    1525                                                 $key = self::debug_var($key).' => '; 
     1556                                                $key = self::debug_var($key, TRUE).' => '; 
    15261557                                        } 
    15271558                                        else 
     
    15291560                                                $key = ''; 
    15301561                                        } 
    1531                                         $out .= ($more ? ', ' : '').$key.self::debug_var($val); 
     1562                                        $out .= ($more ? ', ' : '').$key.self::debug_var($val, TRUE); 
    15321563                                        $more = TRUE; 
    15331564                                } 
    15341565                                return $out.')'; 
    1535  
    15361566                        case 'string': 
    15371567                                return "'$var'"; 
    1538  
    15391568                        case 'float': 
    15401569                                return number_format($var, 6).'…'; 
    1541  
    15421570                        case 'boolean': 
    15431571                                return $var === TRUE ? 'TRUE' : 'FALSE'; 
    1544  
    15451572                        default: 
    15461573                                return (string) $var;