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/07/35 11:47

"gg9h0st" <mn9h0st@hotmail.com> wrote in message
news:1147413633.569854.232010@j33g2000cwa.googlegroups.com...
> i'm a newbie studying php.
>
> i was into array part on tutorial and it says
>
> i'll get an array having keys that from member variable's name
>
> by converting an object to array.
>
> i guessed "i can get public members but not protected, private, static
> members"
> (just like i got error if i try to access protected, private members
> outside of object)
>
> then i wrote a simple code to see how's it going.
>
> ################## code #######################
> <?php
> // for testing, each member of public, protected, private, static
> CLASS test {
> public $a = 'a';
> protected $b = 'b';
> private $c = 'c';
> static $d = 'd';
> }
>
> //converting an object to array
> $a = (array)new test();
>
> var_dump($a);
> echo '<br>';
> /*let's see how it works (it threw not static member cuz it's not in
> the object as i guessed. but it prints all other members including
> public, protected, private eventhough i called this func outside of
> object)*/
>
> //i wanted to try to access by another way and it works too.
> foreach($a as $i => $j)
> echo "\$a : '$i' => '$j'<br>\n";
>
> //i tryed to access by the key i saw on result. but it doesn't work at
> all.
> echo $a[' test c'] . "<br>\n";
> ?>
>
> ################## output #######################
> array(3) {
> ["a"]=>
> string(1) "a"
> [" * b"]=>
> string(1) "b"
> [" test c"]=>
> string(1) "c"
> }
> <br>$a : 'a' => 'a'<br>
> $a : ' * b' => 'b'<br>
> $a : ' test c' => 'c'<br>
> <br>
>
> #################################################
>
>
> if i try to access protected, private members directly outside of the
> object, i'll get an error.
>
> but by convering the object to array, i can get all member variables.
>
> is it right way? then what about the visibility. i think i can be a
> problem.
>
> Is it right way to access object members?(by converting to
> array)<<----------------------------- Q1.

You're not accessing the members of the object anymore. It's an array, not
an object. The structure and values of the array are casted from an object,
but it's still an array and arrays have no restrictions like private or
protected. The answer simply to everything is that once you cast an object
to an array, the whole object context is thrown away and you are dealing
with an array. So you are really not accessing protected members of object
'cos it is not the object any longer.

> anyway i tried to access by typing the key i saw on output ' * b' , '
> test c', it didn't work.
>
> while with the key variables i got from 'foreach', it worked.
>
> i think i made a mistake on making the keys.
>
> so
>
> Can anyone explain me what's the problem on accessing by key i
> typed?<<------------------Q2.

That's a difficult question. I see nothing wrong in it. Does $a['a'] work
either?

--
"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

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