5

OBS: I coded directly here, beacause my code is much more complex.

If I code:

class SuperFoo {
    public function __get($name) {
        return $this->$name;
    }
    public function __set($name, $value) {
        $this->$name = $value;
    }
}

class Foo extends SuperFoo {
    private $bar = '';
}

$foo = new Foo();
$foo->bar = "Why it doesn't work?";
var_dump($foo);

Results in:

object(Foo) {
    ["bar":"Foo":private]=> 
        string(0) ''
}

And not in:

object(Foo) {
    ["bar":"Foo":private]=> 
        string(20) 'Why it doesn't work?'
}

Why this happen? I don't want to use an array to hold the attributes, because I need them declared as individual private members.

5
  • 1
    On the code sample you provided class Foo doesn't even extend SuperFoo, so this is supposed to happen. Check the code you provided, please. Commented Apr 15, 2013 at 16:20
  • 1
    you missed the public function in front of __get and __set. Commented Apr 15, 2013 at 16:20
  • I got: Fatal error: Cannot access private property Foo::$bar Commented Apr 15, 2013 at 16:21
  • That doesn't even compile Commented Apr 15, 2013 at 16:21
  • Sorry, I coded directly because my code is much more complex. But the problem is there. I'll correct it. Commented Apr 15, 2013 at 16:22

3 Answers 3

6

Your code should be resulting in a fatal error because you're trying to access a private property. Even before that, you should receive a syntax error because you haven't properly declared your functions. Hence, you're "resulting" var dump can never occur.

Edit:

You've edited your question. The reason it doesn't work is because bar is private to Foo (SuperFoo cannot access it). Make it protected instead.

Sign up to request clarification or add additional context in comments.

3 Comments

I updated the question. Please, read the update, maybe you can help me.
And we get no Cannot access private property because SuperFoo is creating its own $bar attribute?
I don't believe so, Álvaro. PHP will not create a property with more visibility than is in its subclass (dynamically created properties would be public but it's already private in Foo). It appears to just silently fail.
3

__get($name) isn't called if object has attribute called $name, but it tries to use the attribute directly. And your attribute is private, thus error.

__set() is run when writing data to inaccessible properties.

__get() is utilized for reading data from inaccessible properties.

2 Comments

Thank you for understanding the problem.
I can't see how this answers the question. There's no error triggered in the original code—instead, the attribute modification is apparently ignored.
1

If Foo::bar is private, Foo needs to override __get and __set. This is because SuperFoo can't access private members of Foo. The following code works but it's ugly:

class SuperFoo {
    public function __get($name) {
        return $this->$name;
    }

    public function __set($name, $value) {
        $this->$name = $value;
    }
}

class Foo extends SuperFoo {
    private $bar = '';

    public function __get($name) {
        return $this->$name;
    }

    public function __set($name, $value) {
        $this->$name = $value;
    }
}

The proper solution is to modify Foo::bar visibility to protected. Now SuperFoo has access to Foo::bar so there's no need to override __get and __set in Foo.

class SuperFoo {
    public function __get($name) {
        return $this->$name;
    }

    public function __set($name, $value) {
        $this->$name = $value;
    }
}

class Foo extends SuperFoo {
    protected $bar = '';
}

Check out the PHP Documentation on visibility

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.