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. | | | | re: Basic Member Visibility Question
On Jun 8, 8:38 pm, anonymous <blockst...@gmail.comwrote: Quote:
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 | | | | re: Basic Member Visibility Question
anonymous wrote: Quote:
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. jstucklex@attglobal.net
================== | | | | re: Basic Member Visibility Question
On Jun 8, 8:53*pm, Jerry Stuckle <jstuck...@attglobal.netwrote: Quote:
anonymous wrote: Quote:
I'm having some trouble understanding the meaning of visibility.
> > Quote:
class foobar {
* * private $key1 = "cat";
* * private $key2 = "apple";
* * protected $key3 = "book";
* * public $key4 = 42;
}
> Quote:
$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;
> Quote:
// However, the following works fine
$a = (array) $obj;
print_r( $a );
> Quote:
array_values( $a );
foreach( $a as $val )
* * echo "$val\n";
> > Quote:
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 ));
} | | | | re: Basic Member Visibility Question
anonymous wrote: Quote:
On Jun 8, 8:53 pm, Jerry Stuckle <jstuck...@attglobal.netwrote: Quote:
>anonymous wrote: Quote:
>>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. Quote:
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. jstucklex@attglobal.net
================== | | | | re: Basic Member Visibility Question
anonymous schreef: Quote:
On Jun 8, 8:53 pm, Jerry Stuckle <jstuck...@attglobal.netwrote: Quote:
>anonymous wrote: Quote:
>>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 Quote:
>
function get_object_values( $any_object ) {
$values = (array)$any_object;
return (array_values( $values ));
}
| | | | re: Basic Member Visibility Question
On Jun 9, 5:45*am, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote: Quote:
anonymous schreef:
>
>
> Quote:
On Jun 8, 8:53 pm, Jerry Stuckle <jstuck...@attglobal.netwrote: Quote:
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.
> Quote: Quote:
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.
> Quote: Quote:
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
> Quote:
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
>
>
> Quote:
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. | | | | re: Basic Member Visibility Question
anonymous wrote: Quote:
On Jun 9, 5:45 am, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote: Quote:
>anonymous schreef:
>>
>>
>> Quote:
>>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
>>
>>
>> Quote:
>>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. jstucklex@attglobal.net
================== | | | | re: Basic Member Visibility Question
..oO(anonymous) Quote:
>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 |  | | | | /bytes/about
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 226,471 network members.
|