473,396 Members | 1,789 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,396 software developers and data experts.

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 1172
On Jun 8, 8:38 pm, anonymous <blockst...@gmail.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(inheritance)
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*******@attglobal.net
==================

Jun 27 '08 #3
On Jun 8, 8:53*pm, Jerry Stuckle <jstuck...@attglobal.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...@attglobal.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_values( $any_object ) {
$values = (array)$any_object;
return (array_values( $values ));
}
Jun 27 '08 #4
anonymous wrote:
On Jun 8, 8:53 pm, Jerry Stuckle <jstuck...@attglobal.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...@attglobal.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_values( $any_object ) {
$values = (array)$any_object;
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*******@attglobal.net
==================

Jun 27 '08 #5
anonymous schreef:
On Jun 8, 8:53 pm, Jerry Stuckle <jstuck...@attglobal.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...@attglobal.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_values( $any_object ) {
$values = (array)$any_object;
return (array_values( $values ));
}
Jun 27 '08 #6
On Jun 9, 5:45*am, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
anonymous schreef:
On Jun 8, 8:53 pm, Jerry Stuckle <jstuck...@attglobal.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...@attglobal.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_values( $any_object ) {
* * $values = (array)$any_object;
* * 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_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
>anonymous schreef:
>>On Jun 8, 8:53 pm, Jerry Stuckle <jstuck...@attglobal.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...@attglobal.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_values( $any_object ) {
$values = (array)$any_object;
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*******@attglobal.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
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...
9
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)...
16
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
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...
4
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. -...
14
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...
12
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...
9
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
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...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
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...
0
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...
0
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...

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.