473,785 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Basic Member Visibility Question

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;
}

$obj = new foobar();
// Trying to print the keys results in a PHP fatal error
echo $obj->$key1;
// Trying to make a copy of the keys results in a PHP fatal error
$a = $obj->$key1;

// However, the following works fine
$a = (array) $obj;
print_r( $a );

array_values( $a );
foreach( $a as $val )
echo "$val\n";

$b = $a[0];
?>

I don't see what the point of the errors are if the results can be
accomplished just as easily in
some other fashion.
Jun 27 '08 #1
8 1204
On Jun 8, 8:38 pm, anonymous <blockst...@gma il.comwrote:
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;

}

$obj = new foobar();
// Trying to print the keys results in a PHP fatal error
echo $obj->$key1;
// Trying to make a copy of the keys results in a PHP fatal error
$a = $obj->$key1;

// However, the following works fine
$a = (array) $obj;
print_r( $a );

array_values( $a );
foreach( $a as $val )
echo "$val\n";

$b = $a[0];
?>

I don't see what the point of the errors are if the results can be
accomplished just as easily in
some other fashion.
you can't directly access a private attribute from outside a class (or
anything for that matter)

you need to use get/set methods to access it outside of the class
(read outside of class foobar {} )

public function getKeys() {
return array($this->key1, $this->key2, $this->key3, $this->key4);
}

or

function _getKey1() (for the purist :p)

and then do $obj = new foobar();

$a = $obj->getKeys();

public -anywhere
protected-inside the class and child classes(inherit ance)
private-only within the class it's self
Jun 27 '08 #2
anonymous wrote:
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;
}

$obj = new foobar();
// Trying to print the keys results in a PHP fatal error
echo $obj->$key1;
// Trying to make a copy of the keys results in a PHP fatal error
$a = $obj->$key1;

// However, the following works fine
$a = (array) $obj;
print_r( $a );

array_values( $a );
foreach( $a as $val )
echo "$val\n";

$b = $a[0];
?>

I don't see what the point of the errors are if the results can be
accomplished just as easily in
some other fashion.
A private member is the way PHP handles the Object Oriented Programming
(OOP) concept of *encapsulation* , one of the four attributes of Object
Oriented programming. There are several advantages to it, including
protecting the member from unauthorized changes (changes must be made
through member functions), and isolating the implementation of the
object from the interface.

OOP is an entire methodology for design and implementation of programs,
which in many ways is superior to non-OO methods (but can have it's own
limitations, also). Entire books have been written about it. You
really need to visit a bookstore/library and study up on OO concepts.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Jun 27 '08 #3
On Jun 8, 8:53*pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
anonymous wrote:
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;
}
$obj = new foobar();
// Trying to print the keys results in a PHP fatal error
echo $obj->$key1;
// Trying to make a copy of the keys results in a PHP fatal error
$a = $obj->$key1;
// However, the following works fine
$a = (array) $obj;
print_r( $a );
array_values( $a );
foreach( $a as $val )
* * echo "$val\n";
$b = $a[0];
?>
I don't see what the point of the errors are if the results can be
accomplished just as easily in
some other fashion.

A private member is the way PHP handles the Object Oriented Programming
(OOP) concept of *encapsulation* , one of the four attributes of Object
Oriented programming. *There are several advantages to it, including
protecting the member from unauthorized changes (changes must be made
through member functions), and isolating the implementation of the
object from the interface.

OOP is an entire methodology for design and implementation of programs,
which in many ways is superior to non-OO methods (but can have it's own
limitations, also). *Entire books have been written about it. *You
really need to visit a bookstore/library and study up on OO concepts.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attgl obal.net
=============== ===
I assumed that the keyword private, and similarly protected, were
meant to do more than just prevent a class member from being modified
from outside of the class (by foreign functions).
The inability to print or copy a private property's value implies that
the value is hidden, and therefore not expected to be visible from
outside of the class.
Yet, any value of an object, whether private or protected, can be
easily be seen by converting the object to an array. The array not
only holds the values, but also the names of each member.
Does every OOP language allow such easy access to private properties?

function get_object_valu es( $any_object ) {
$values = (array)$any_obj ect;
return (array_values( $values ));
}
Jun 27 '08 #4
anonymous wrote:
On Jun 8, 8:53 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
>anonymous wrote:
>>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;
}
$obj = new foobar();
// Trying to print the keys results in a PHP fatal error
echo $obj->$key1;
// Trying to make a copy of the keys results in a PHP fatal error
$a = $obj->$key1;
// However, the following works fine
$a = (array) $obj;
print_r( $a );
array_value s( $a );
foreach( $a as $val )
echo "$val\n";
$b = $a[0];
?>
I don't see what the point of the errors are if the results can be
accomplishe d just as easily in
some other fashion.
A private member is the way PHP handles the Object Oriented Programming
(OOP) concept of *encapsulation* , one of the four attributes of Object
Oriented programming. There are several advantages to it, including
protecting the member from unauthorized changes (changes must be made
through member functions), and isolating the implementation of the
object from the interface.

OOP is an entire methodology for design and implementation of programs,
which in many ways is superior to non-OO methods (but can have it's own
limitations, also). Entire books have been written about it. You
really need to visit a bookstore/library and study up on OO concepts.

--
============== ====
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attg lobal.net
============== ====

I assumed that the keyword private, and similarly protected, were
meant to do more than just prevent a class member from being modified
from outside of the class (by foreign functions).
The inability to print or copy a private property's value implies that
the value is hidden, and therefore not expected to be visible from
outside of the class.
That is correct. It is separating the interface from the implementation.
Yet, any value of an object, whether private or protected, can be
easily be seen by converting the object to an array. The array not
only holds the values, but also the names of each member.
Does every OOP language allow such easy access to private properties?

function get_object_valu es( $any_object ) {
$values = (array)$any_obj ect;
return (array_values( $values ));
}
Sure. But then it's not a class object. You aren't accessing the class
properties - you are accessing a copy of that data.

And you can access the private properties one way or another in most
languages. OO is about protection, not security. If you bypass the OO
design, you lose many of the advantages of OO programming.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Jun 27 '08 #5
anonymous schreef:
On Jun 8, 8:53 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
>anonymous wrote:
>>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;
}
$obj = new foobar();
// Trying to print the keys results in a PHP fatal error
echo $obj->$key1;
// Trying to make a copy of the keys results in a PHP fatal error
$a = $obj->$key1;
// However, the following works fine
$a = (array) $obj;
print_r( $a );
array_value s( $a );
foreach( $a as $val )
echo "$val\n";
$b = $a[0];
?>
I don't see what the point of the errors are if the results can be
accomplishe d just as easily in
some other fashion.
A private member is the way PHP handles the Object Oriented Programming
(OOP) concept of *encapsulation* , one of the four attributes of Object
Oriented programming. There are several advantages to it, including
protecting the member from unauthorized changes (changes must be made
through member functions), and isolating the implementation of the
object from the interface.

OOP is an entire methodology for design and implementation of programs,
which in many ways is superior to non-OO methods (but can have it's own
limitations, also). Entire books have been written about it. You
really need to visit a bookstore/library and study up on OO concepts.

--
============== ====
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attg lobal.net
============== ====

I assumed that the keyword private, and similarly protected, were
meant to do more than just prevent a class member from being modified
from outside of the class (by foreign functions).
The inability to print or copy a private property's value implies that
the value is hidden, and therefore not expected to be visible from
outside of the class.
Yet, any value of an object, whether private or protected, can be
easily be seen by converting the object to an array. The array not
only holds the values, but also the names of each member.
Does every OOP language allow such easy access to private properties?
Well, you are not really accessing them, merely reading.
But yes, I expect you can change them too in a similar fashion.

The point is: it doesn't matter too much anyway.
OOP is ment as another way to program in a structured way; OOP is not a
security measure.
So 'protected' and 'private' are NOT security measures, but
language/structure concepts in OOP.

Just buy a good book on OOP, like Jerry suggested, and these questions
disappear. :-)

Good luck.

Regards,
Erwin Moller
>
function get_object_valu es( $any_object ) {
$values = (array)$any_obj ect;
return (array_values( $values ));
}
Jun 27 '08 #6
On Jun 9, 5:45*am, Erwin Moller
<Since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
anonymous schreef:
On Jun 8, 8:53 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
anonymous wrote:
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;
}
$obj = new foobar();
// Trying to print the keys results in a PHP fatal error
echo $obj->$key1;
// Trying to make a copy of the keys results in a PHP fatal error
$a = $obj->$key1;
// However, the following works fine
$a = (array) $obj;
print_r( $a );
array_values ( $a );
foreach( $a as $val )
* * echo "$val\n";
$b = $a[0];
?>
I don't see what the point of the errors are if the results can be
accomplished just as easily in
some other fashion.
A private member is the way PHP handles the Object Oriented Programming
(OOP) concept of *encapsulation* , one of the four attributes of Object
Oriented programming. *There are several advantages to it, including
protecting the member from unauthorized changes (changes must be made
through member functions), and isolating the implementation of the
object from the interface.
OOP is an entire methodology for design and implementation of programs,
which in many ways is superior to non-OO methods (but can have it's own
limitations, also). *Entire books have been written about it. *You
really need to visit a bookstore/library and study up on OO concepts.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attgl obal.net
=============== ===
I assumed that the keyword private, and similarly protected, were
meant to do more than just prevent a class member from being modified
from outside of the class (by foreign functions).
The inability to print or copy a private property's value implies that
the value is hidden, and therefore not expected to be visible from
outside of the class.
Yet, any value of an object, whether private or protected, can be
easily be seen by converting the object to an array. The array not
only holds the values, but also the names of each member.
Does every OOP language allow such easy access to private properties?

Well, you are not really accessing them, merely reading.
But yes, I expect you can change them too in a similar fashion.

The point is: it doesn't matter too much anyway.
OOP is ment as another way to program in a structured way; OOP is not a
security measure.
So 'protected' and 'private' are NOT security measures, but
language/structure concepts in OOP.

Just buy a good book on OOP, like Jerry suggested, and these questions
disappear. :-)

Good luck.

Regards,
Erwin Moller
function get_object_valu es( $any_object ) {
* * $values = (array)$any_obj ect;
* * return (array_values( $values ));
}

Reading data is a definite form of access.
Okay, so I read a little further in the manual and figured out how
to prevent this from happening. I just needed to declare the
properties
both static and private, and then use protected methods to access
them.
<?php
class foo {
private static $key = "password";

protected function show() {
echo( self::$key );
}
}
?>
The member $key is completely hidden as far as I can tell.
Jun 27 '08 #7
anonymous wrote:
On Jun 9, 5:45 am, Erwin Moller
<Since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
>anonymous schreef:
>>On Jun 8, 8:53 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
anonymous wrote:
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;
}
$obj = new foobar();
// Trying to print the keys results in a PHP fatal error
echo $obj->$key1;
// Trying to make a copy of the keys results in a PHP fatal error
$a = $obj->$key1;
// However, the following works fine
$a = (array) $obj;
print_r( $a );
array_value s( $a );
foreach( $a as $val )
echo "$val\n";
$b = $a[0];
?>
I don't see what the point of the errors are if the results can be
accomplishe d just as easily in
some other fashion.
A private member is the way PHP handles the Object Oriented Programming
(OOP) concept of *encapsulation* , one of the four attributes of Object
Oriented programming. There are several advantages to it, including
protecting the member from unauthorized changes (changes must be made
through member functions), and isolating the implementation of the
object from the interface.
OOP is an entire methodology for design and implementation of programs,
which in many ways is superior to non-OO methods (but can have it's own
limitation s, also). Entire books have been written about it. You
really need to visit a bookstore/library and study up on OO concepts.
--
============ ======
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@at tglobal.net
============ ======
I assumed that the keyword private, and similarly protected, were
meant to do more than just prevent a class member from being modified
from outside of the class (by foreign functions).
The inability to print or copy a private property's value implies that
the value is hidden, and therefore not expected to be visible from
outside of the class.
Yet, any value of an object, whether private or protected, can be
easily be seen by converting the object to an array. The array not
only holds the values, but also the names of each member.
Does every OOP language allow such easy access to private properties?
Well, you are not really accessing them, merely reading.
But yes, I expect you can change them too in a similar fashion.

The point is: it doesn't matter too much anyway.
OOP is ment as another way to program in a structured way; OOP is not a
security measure.
So 'protected' and 'private' are NOT security measures, but
language/structure concepts in OOP.

Just buy a good book on OOP, like Jerry suggested, and these questions
disappear. :-)

Good luck.

Regards,
Erwin Moller
>>function get_object_valu es( $any_object ) {
$values = (array)$any_obj ect;
return (array_values( $values ));
}

Reading data is a definite form of access.
Okay, so I read a little further in the manual and figured out how
to prevent this from happening. I just needed to declare the
properties
both static and private, and then use protected methods to access
them.
<?php
class foo {
private static $key = "password";

protected function show() {
echo( self::$key );
}
}
?>
The member $key is completely hidden as far as I can tell.
Just remember - encapsulation is about PROTECTION, not SECURITY. Don't
depend on it to protect sensitive data (like passwords).

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Jun 27 '08 #8
..oO(anonymous)
>Reading data is a definite form of access.
Okay, so I read a little further in the manual and figured out how
to prevent this from happening. I just needed to declare the
properties
both static and private, and then use protected methods to access
them.
<?php
class foo {
private static $key = "password";

protected function show() {
echo( self::$key );
}
}
?>
The member $key is completely hidden as far as I can tell.
Only in OOP context. There are still other ways to access them in almost
every language, even in strictly-typed ones. A variable or an instance
is just a kind of data structure in memory, which can be interpreted and
introspected in various ways (for example with the Reflection API).

Another example would be a filesystem. Every modern filesystem assigns
various access privileges to the files and the OS checks if I'm allowed
to work with the file or not. But even if the access is denied, it can't
prevent me from directly reading the harddisk sectors with another tool
to get the data I want (as long as it's not encrypted, though).

Micha
Jun 27 '08 #9

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

Similar topics

4
2815
by: Jonathan | last post by:
Hi, I've read through quite a number of postings on here so far and have seen what look like very simply, reasonable answers to this question, however I am still completely unable to do what I want to do. I just want to know how I should toggle the visibility of divs in Netscape (I'm using 7). For example, say I have the following HTML code:
9
2318
by: Christian E. Böhme | last post by:
Hello all, I ran into a little problem with recursive templates that I am not sure what it has to do with, essentially, since I am currently limited in my access to compilers (namely GCC 4.1) and until now had no chance of testing the code with others. It may be an implementation detail or even in the standard (which I have no access to, unfortunately). Or maybe I have hit one of those cases whose solutions are "undefined" by the...
16
3632
by: Fir5tSight | last post by:
Hi All, I have a small C#.NET program that is as follows: using System; class A { protected int x = 123; }
52
20912
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member 'UselessJunkForDissassembly.IInvocableInternals.OperationValidate(string)' C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
4
2648
by: Goran Djuranovic | last post by:
Hi all, I am experiencing a strange thing happening with a "designer.vb" page. Controls I manually declare in this page are automatically deleted after I drop another control on a ".aspx" page. - Why is this happening? - Can I disable automatic declaration and have everything be declared manually? - Any other options to fix this? Thanks in advance. Goran Djuranovic
14
1852
by: MartinRinehart | last post by:
Working on parser for my language, I see that all classes (Token, Production, Statement, ...) have one thing in common. They all maintain start and stop positions in the source text. So it seems logical to have them all inherit from a base class that defines those, but this doesn't work: import tok class code: def __init__( self, start, stop ):
12
1085
by: OccasionalFlyer | last post by:
I am pretty new to JavaScript and having trouble with something that should, I think, be fairly easy. I have one form. I have two radio buttons. I have a text box that is hidden. If you click the radio button that has the value "Yes", the hidden field should be made visible. I would appreciate help knowing what I did wrong. Here is the
9
1607
by: Peskov Dmitry | last post by:
It is a very basic question.Surely i got something wrong in my basic understanding. //Contents of file1.cpp using namespace std; #include <iostream> template <typename T> class my_stack;
3
1227
by: puzzlecracker | last post by:
.... I am coming off c++, so here is the list of question, I'd like to get clarifications. 1. What's and the point of static constructor? 2. Why C# differs as to base class visibility in regard to Derived class. In other words, Base class should be at least visible/ accessible as derived. 3. Can we member hiding by re-implementing the method in the derived
0
9647
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
9489
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
9959
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
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
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();...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4061
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
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.