Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old May 12th, 2006, 07:05 AM
gg9h0st
Guest
 
Posts: n/a
Default visibility matter on converting object to array

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.

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.

  #2  
Old May 12th, 2006, 07:45 AM
Kimmo Laine
Guest
 
Posts: n/a
Default Re: visibility matter on converting object to array

"gg9h0st" <mn9h0st@hotmail.com> wrote in message
news:1147413633.569854.232010@j33g2000cwa.googlegr oups.com...[color=blue]
> 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.[/color]

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.
[color=blue]
> 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.[/color]

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)


  #3  
Old May 12th, 2006, 08:05 AM
gg9h0st
Guest
 
Posts: n/a
Default Re: visibility matter on converting object to array

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

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

  #4  
Old May 12th, 2006, 11:25 AM
Kimmo Laine
Guest
 
Posts: n/a
Default Re: visibility matter on converting object to array

"gg9h0st" <mn9h0st@hotmail.com> wrote in message
news:1147417149.809964.159660@j33g2000cwa.googlegr oups.com...[color=blue]
> 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[/color]

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.
[color=blue]
> and echo $a['a']; works well of course.[/color]

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)


  #5  
Old May 12th, 2006, 11:45 AM
gg9h0st
Guest
 
Posts: n/a
Default Re: visibility matter on converting object to array

Great explanation :D

yes i thought private, protected members can't be read from outside

Thank you Kimmo.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.