04-18-2009, 06:30 PM
|
#11 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by allworknoplay
When/what is the appropriate time to ever set a property to "public" or is it never?
I like the idea of accessing a property value directly, it seems cleaner....or shorter than to have to create a "getter" method to get obtain the data value.
I can also see the security issue side of it, so I guess my question is:
1) why would PHP implement this if there are security issues?
2) is there ever an appropriate time set a property to public?
|
I would say never, but there are obviously situations when you want to use public. The only reason I say it's a security issue is due to the fact that you can edit it aswell as getting the value from it.
If you want to access a variable directly, I would make use of the __get method in PHP5.
PHP Code:
private $variable = array();
public function __get($name) {
if(array_key_exists($name, $variable) {
return $this->variable[$name];
}
}
//use:
$pagination->no_result; //would get value of $variable['no_result'];
__________________
|
|
|
|