Re: [PHP] Object to array conversion oddity

This is a discussion on Re: [PHP] Object to array conversion oddity within the PHP General forums, part of the PHP Programming Forums category; On Saturday 23 September 2006 10:04, Marcus Bointon wrote: > On 23 Sep 2006, at 15:51, Ray Hauge ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-23-2006
Ray Hauge
 
Posts: n/a
Default Re: [PHP] Object to array conversion oddity

On Saturday 23 September 2006 10:04, 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 - if I had happened to have a property called
> 'myclassfield1', I would not be able to tell if the real property
> name was 'myclassfield1' 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.
>
> Marcus
> --
> Marcus Bointon
> Synchromedia Limited: Creators of http://www.smartmessages.net/
> marcus@synchromedia.co.uk | http://www.synchromedia.co.uk/


Could you do something like this?

$private = "Myclass"
$protected = "*";

// public variable
echo $myclass["field1"];

// private variable
echo $myclass[$private."field2"];

// protected variable
echo $myclass[$protected."field3"];

Unless, of course, this behavior is not consistent. If it is a bug, then you
run the risk of it being fixed in the future. But if it is intended to be
that way, then this format should work regardless of what the member variable
names are.

--
Ray Hauge
Programmer/Systems Administrator
American Student Loan Services
www.americanstudentloan.com
1.800.575.1099
Reply With Quote
  #2 (permalink)  
Old 09-23-2006
Marcus Bointon
 
Posts: n/a
Default Re: [PHP] Object to array conversion oddity

On 23 Sep 2006, at 16:37, Ray Hauge wrote:

> Could you do something like this?
>
> $private = "Myclass"
> $protected = "*";


No, because if I have a property called 'Myclassfield1', after
casting to an array I can't tell if it's private property called
'field1' or a public property called 'Myclassfield1'.

I think it's just plain wrong. Arrays should not try to be objects.
You can find out protection level of a property via introspection of
the object that you have in hand. In the vast majority of cases,
you'll want it to act just like the docs say it does, and if you
don't, you should probably be using the object itself anyway.

Bug report is here: http://bugs.php.net/?id=38935

Marcus
--
Marcus Bointon
Synchromedia Limited: Creators of http://www.smartmessages.net/
marcus@synchromedia.co.uk | http://www.synchromedia.co.uk/
Reply With Quote
Reply
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

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



All times are GMT +1. The time now is 08:56 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0