473,385 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.

May 12 '06 #1
4 3164
"gg9h0st" <mn*****@hotmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.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
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
May 12 '06 #2
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.

May 12 '06 #3
"gg9h0st" <mn*****@hotmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.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
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
May 12 '06 #4
Great explanation :D

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

Thank you Kimmo.

May 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification...
12
by: lawrence | last post by:
The following function correctly makes everything invisible but then fails to turn the one chosen DIV back to visible. I imagine I'm getting the syntax of the variable wrong? I've tried this with...
1
by: lkrubner | last post by:
In the first version of this function, I only test for true or false, on a global var, without limiting that true or false to the id of a particular item. What this meant was that if you were using...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
8
by: iyuen | last post by:
I'm having problems with converting a byte array to an image object~ My byte array is an picture in VB6 StdPicture format. I've used propertybag to convert the picture into base64Array format in...
3
by: RitualDave | last post by:
This compiles and runs successfully in VS2005: ref class A { private: ~A() { this->!A(); } // private! !A() { } // private! }; ....
5
by: Peter Michaux | last post by:
There seem to be some options for converting the arguments object inside a function to an instance of Array. I'm curious if anyone has encountered any problems with any particular techniques for...
8
by: anonymous | last post by:
I'm having some trouble understanding the meaning of visibility. <?php class foobar { private $key1 = "cat"; private $key2 = "apple"; protected $key3 = "book"; public $key4 = 42; }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.