|
Posted by Janwillem Borleffs on 10/30/53 11:25
Piro wrote:
> Also, I noticed you wrote a method to return a class property... is
> there any way to access the property directly? Rather than
>
> print $client->getName();
>
> I was thinking
>
> print $client->name;
>
> It seems cumbersome to have methods for each class property, especially
> if there are a bunch of them.
>
Perhaps, but in general it's considered bad practise to access class
properly without a getter, unless they are defined as public statics.
Anyways, when you don't want to define setters, you can use overloading
by adding the following method to your class:
public function __call($function, $arguments) {
$function = strtolower($function);
$var = preg_replace('/^get/', '', $function);
if (isset($this->$var)) {
return $this->$var;
}
}
This way you can use method calls like $client->getName() to get a
property with the name $name, $client->getAnother_name() to get a
property with the name $another_name etcetera.
And, as you have might have guessed by now, there isn't a way to access
teh class properties directly as setClass() only exports class methods.
JW
Navigation:
[Reply to this message]
|