You are here: Re: visibility matter on converting object to array « PHP Programming Language « IT news, forums, messages
Re: visibility matter on converting object to array

Posted by Kimmo Laine on 11/19/11 11:47

"gg9h0st" <mn9h0st@hotmail.com> wrote in message
news:1147417149.809964.159660@j33g2000cwa.googlegroups.com...
> thanks for replying.
>
> yea surly i work with array not object. but i still feel like it's not
> that normal i can access protected, private members of the object
> outside of the object eventhough by casting to an array. hm but with
> the fact 'php is not an oop', anyway i can understand. :P

You're missing the point now. :) It's still not the _object_ you are
accessing. Think like this: you can't stick a needle through a plate of
steel. If you take a photograph of the steel plate and stick a needle
through the photograph, it doesn't mean you're sticking it through the
steel, you're sticking it in the phtograph. And the proof is that there is a
hole in the picture but no hole in the steel.

Your concern seems to be that you can read the values of private object
memebers outside the object by casting it to array. Sure, but you can't
write to them anything, and that's what matters.

class foo {
private $bar='zap';
}

$f = new foo();
$cast = (array)$f;
echo $cast[' foo bar']; // [' foo bar'] doesn't work really but assume it
does
$cast['bar']='baz';

At this point the value of $f->bar is still 'zap', not 'baz', because the
value altered was that of the array and not the object. The object remains
intact the whole time. You can read the value but can't write anything to
it.

This of course is somewhat an issue, perhaps someone might disagree that a
private class member should not be read either outside the class, now it
simply cannot be written to. Anyway, it might be reasonable to mention this
at least in the manual for example in the chapter about casting.

> and echo $a['a']; works well of course.

the spaces may not have been spaces but some control characters instead. I
tested the same and got some sort of boxes instead of spaces...

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация