Posted by Michael Fesser on 05/07/07 15:31
..oO(cluthz)
>I'm going to create a class in one of my PHP4 applications for the first
>time. However I was reading up about the
>__get and __set functions within a class.
__get() and __set() are magic functions in PHP5, they don't exist in
PHP4.
>Reading about OO / classes I would expect to be able to create a get
>function for each public variable.
>
>Therefore if I has class example:
>
>class example {
>
> var $test1;
> var $test2;
>
>}
>
>I would expect to be able to add the functions (something like the
>following)
>__get ($test1) {
> #Do stuff with test1
> return $this->$test1;
>}
>
>__get ($test2) {
> #Do stuff with test2
> return $this->$test2;
>}
You getter and setter methods would have to look like
function getTest1() {
return $this->test1;
}
function setTest1($test1) {
$this->test1 = $test1;
}
>But reading about it, it seems that I can only have a single __get function
>per class and that within the __get function I have to test for which
>variable is being set.
>
>e.g.
>__get ($name) {
>
> switch ($name) {
> case "test1":
> #Do stuff with test1
> return $this->$test1;
> case "test2":
> #Do stuff with test2
> return $this->$test3;
> case default:
> echo "error";
> }
>}
That's what __get() is for - it is called whenever you try to access a
property that doesn't exist, for exampe $test3 in your example above:
print $someObj->test3;
would invoke the magic __get() method with "test3" passed as its
parameter.
>This is not what I would expect of OO behaviour from what I have read
>elsewhere.
Read again. ;)
Micha
Navigation:
[Reply to this message]
|