473,735 Members | 7,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3193
"gg9h0st" <mn*****@hotmai l.com> wrote in message
news:11******** **************@ j33g2000cwa.goo glegroups.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***@bhg byrzcv.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*****@hotmai l.com> wrote in message
news:11******** **************@ j33g2000cwa.goo glegroups.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***@bhg byrzcv.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
29679
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 says: "The 'visibility' property takes the value 'collapse' for row, row group, column, and column group elements. This value causes the entire row or column to be removed from the display, and the space normally taken up by the row or column to...
12
3940
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 both single quotes and no single quotes, but it doesn't work either way. What am I doing wrong? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) { document.getElementById('weblogs').style.visibility='hidden';
1
1809
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 this function for several items on a page, sometimes you had to click twice and other times you only had to click once, because sometimes visibility was false though you were clicking on an item that was true. So then I started adding the id's...
8
3375
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 actual program. To me "scope" of the name of a function or object are the general rules for the areas of a program that can through a declaration, have "visibility."
8
4565
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 XML, and embedded the array as some child element in an xml file, i.e.: <Mask>bHQAAH4AAABCTX4AAAAAAAAAPgAAACgAAAAQAAAAEAAAAAEAAQAAAAAAQAAAAAAAAAAAAA AA AAAAAAAAAAAAAAAA////AP//AAD//wAA//8AAP//AAD/7wAA//cAALtzAABVeQAAVUAAAFVA...
3
2465
by: RitualDave | last post by:
This compiles and runs successfully in VS2005: ref class A { private: ~A() { this->!A(); } // private! !A() { } // private! }; ....
5
1670
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 this From <URL: http://groups.google.com/group/comp.lang.javascript/msg/98c27152ea5c7d55?dmode=source> var args = .slice.call(arguments,1); var args = Array.prototype.slice.call(arguments, 1); I've tried the following which is short and it...
8
1203
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; }
0
8962
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8785
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9463
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9327
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9251
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6049
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3273
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2739
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.