Re: Object to array conversion oddity

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, 2006-09-23 at 16:04 +0100, Marcus Bointon wrote:
> On 23 Sep 2006, at 15:51, Ray Hauge wrote:
> 
> > To me it looks like they append the name of the class to any private
> > variables.  I would guess that it does this to make sure you know  
> > what you're
> > doing and using the private variable like that.  I'm  just guessing  
> > at that
> > point though.
> 
> Well, I realised that, but it's not very helpful as there's no  
> separator between class name and variable name it's impossible to  
> separate it correctly

<?php

class MyClass()
{
    public $field1 = '';
    private $field2 = '';

    public function __sleep()
    {
        return array('field1', 'field2');
    }
}

$myClass = new Myclass;
$prefix = get_class( $myClass );
$prefixL = strlen( $prefix );

$myArray = (array)$myClass;
foreach( $myArray as $name => $value )
{
    if( substr( $name, 0, 1 ) === '*' )
    {
        $foo = &$myArray[$name];
        unset( $myArray[$name] );
        $myArray[substr( $name, 1 )] = &$foo;
    }
    else
    if( ereg( "^$prefix", '', $name ) )
    {
        $foo = &$myArray[$name];
        unset( $myArray[$name] );
        $myArray[substr( $name, $prefixL )] = &$foo;
    }
}

?>

>  - if I had happened to have a property called  
> 'myclassfield1', I would not be able to tell if the real property  
> name was 'myclassfield1'

And the likelihood of you having a property called Myclassfield1 is?
Most developers start a class name with a capital, and a property name
with a lowercase. So the likelihood if you are adhering to convention is
slim to squat. That said, I think they should have put in a -> or a :
character :)

>  or a private property called 'field1'. If it  
> is going to stray from the documented behaviour, It would be far more  
> useful to retain the names that the var_dump on the object uses -  
> 'field1:private'. That's safe as : is not a value char in a variable  
> name, but it's fine as an array index.
> 
> > Try a test with multiple public and multiple private variables.  If  
> > the format
> > of the array keys stays the same, then you should have your answer.
> 
> In my real code I do have multiple fields all exhibiting this  
> behaviour. I'll report it.

It sounds like they've helped out by giving more data than was
necessary. This can be useful when working with the data to know it's
origin.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux