Re: OOP, classes and databases [message #169418 is a reply to message #169414] |
Sat, 11 September 2010 13:45 |
aaaa
Messages: 7 Registered: September 2010
Karma:
|
Junior Member |
|
|
>> - __get and __set is the way for getting and setting properties
> No. Usually properties are private and you write getFoo() and setFoo(
> $value ) methods to retrieve and set their values in a controlled way
> (allowing to validate the value to be set, for example). There may be
> circumstances where it is okay to allow anyone unrestricted access to a
> property. In this case, you can make the property public and don't need
> __get/__set. If you want to control access to a property, but still allow
> the simpler syntax $object->foo = 42, then you can use __get/__set. Also,
> if you want to allow public read-only access to a private property, you
> can use __get to achieve that.
Methods __get and __set are OK, if you use them like this:
public function __set($var, $val)
{
switch ($var)
{
case 'name1' : //validation here
break;
case 'name2' :
case 'name3' : //validation here
break;
}
$this->variables[$var] = $val;
}
--
A
|
|
|