TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-11-2008, 08:27 PM   #1 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default Accessing data in protected and private properties

Today I decided to look though a small trick by Derick Rethans on how to access private properties in PHP and I ended up making a function to collect Reflection-like information and wanted to share the source:

PHP Code:
<?php
    
/**
     * Reflector
     *
     * Provides reflection-like information to expose an 
     * objects properties.
     *
     * Based on trick to access private properties by 
     * Derick Rethans.
     *
     * @param    object        The object to inspect
     * @return    array        Returns an array on succes and false on error
     */
    
function Reflector($object)
    {
        if(!
is_object($object))
        {
            return(
false);
        }

        
$retval = Array(
                
'class'        => get_class($object), 
                
'public'    => Array(), 
                
'protected'    => Array(), 
                
'private'    => Array()
                );

        
$cast = (array) $object;

        if(!
sizeof($cast))
        {
            return(
$retval);
        }

        
$class         get_class($object);
        
$class_len    strlen($class);

        foreach(
$cast as $property => $value)
        {
            if(
$property{0} == "\0")
            {
                if(
$property{1} == '*')
                {
                    
$retval['protected'][substr($property3)] = $value;
                }
                elseif(
substr($property1$class_len) == $class)
                {
                    
$retval['private'][substr($property$class_len 2)] = $value;
                }
            }
            else
            {
                
$retval['public'][$property] = $value;
            }
        }

        return(
$retval);
    }
?>
To explain the trick in detail, the main trick is in this line:
PHP Code:
        ...
        
$cast = (array) $object;
        ... 
When casting an object to an array it will expose the values of its properties no matter of the visibility level. All private and protected properties's index will begin with a NUL character (\0). For protected properties the NUL character will be followed by a * character and another NUL character and then its property name. For private properties the first character is a NUL character and then the class name and another NUL character and then its property name. All other indexes are public (either using public or var keywords).

Its also however possible to change the values, BUT when you cast them back to an object the class will be changed to an instance of the 'stdClass' class and therefore it will loose all its methods and isn't useful any longer, so this trick is mainly for debugging proposes ;)

Hope this will help some!
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
The Following User Says Thank You to Kalle For This Useful Post:
RobertK (09-30-2008)
Old 09-11-2008, 08:34 PM   #2 (permalink)
Jim
The Addict
 
Jim's Avatar
 
Join Date: Nov 2007
Location: the Netherlands
Posts: 281
Thanks: 2
Jim is on a distinguished road
Default

As expected from Kalle, some very nice piece of PHP. :)
__________________
Nunchaku! Who doesn't like martial arts? =)
Send a message via MSN to Jim Send a message via Skype™ to Jim
Jim is offline  
Reply With Quote
Old 09-12-2008, 08:56 PM   #3 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

On a side note. If you have wrote your program in such a way that you ever need to bring private class variables outside of the class, you need to rethink your design. Private variables are made for the specific purpose of not being seen by anything, so to need to see them is a sign of bad architecture.
__________________

Village Idiot is offline  
Reply With Quote
Old 09-12-2008, 11:59 PM   #4 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

The code was written for debugging proposes where it sometimes is needed to check the values of private/protected properties to make sure everything is in place.
__________________

Last edited by Kalle : 09-15-2008 at 11:12 AM.
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
The Following User Says Thank You to Kalle For This Useful Post:
tony (09-22-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:38 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design