2

Ok, so here's a snippet of my Properties base class, that is extended in most of the classes in my application:

class Properties {
    protected $properties_arr = array();

    /**
     * Magic function to be able to access the object's properties
     * @param string $property
     */
    public function __get( $property ) {
        if ( array_key_exists( $property, $this->properties_arr ) ) {
            return $this->properties_arr[ $property ];
        }

        return $this->getUndefinedProperty( $property );
    }

    /**
     * Magic function to be able to access the object's properties
     * @param string $property
     * @param mixed $value
     */
    public function __set( $property, $value ) {
        if ( property_exists( $this, $property ) ) {
            $this->setProtectedProperty( $property );
        }

        $this->properties_arr[ $property ] = $value;
    }

It's pretty basic, and there's nothing wrong with it, but I'm running into a problem that I've encountered a couple times before, and it's that you can't perform certain actions on an array property through the __get method.

Doing this, for instance:

$MyClass = new Properties();
$MyClass->test = array();
$MyClass->test['key'] = 'value';

you would expect the $MyClass->test array to contain one item, but it's still an empty array! Now I know I can work around it by just assigning an array with the items already in it, but I would just really like to know why this is (and if there's a better solution).

Thanks!

3
  • What is Properties::setProtectedProperty($var)? Commented Aug 25, 2011 at 14:05
  • Just an internal method that throws an Exception, but can be overridden by a child class to customize the behavior. Do you think this could have something to do with the problem? I'm not overriding it in the class where I'm experiencing the problem. Commented Aug 25, 2011 at 14:09
  • Probably no, it should not be a problem. See @Peter's answer, it works for me. Without it, I got PHP error "Notice: Indirect modification of overloaded property Test::$foo has no effect" (do you have error reporting enabled?) Commented Aug 25, 2011 at 14:15

2 Answers 2

4

See PHP Accessor functions and arrays.

The issue is probably that you need to make the __get() method return a reference.

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

8 Comments

Yup; reckon that's it. See also this bug for a little history. @Rijk, I think that your code should have thrown a warning on your array access -- do you have error reporting turned on?
Ah right, now I understand! I already found this solution in the PHP.net comments, but the author didn't explain it as well as in the post here on SO. The problem for me is, that this triggers a "Only variable references should be returned by reference", because of the conditional getUndefinedProperty() return.. :(
@Matt: I sure do, everything including Notices. It doesn't trigger a warning - could you tell me what error you were expecting?
@Rijk What version of PHP? Having chucked your code at my installation of 5.3.5, I get Notice: Indirect modification of overloaded property Properties::$test has no effect in C:\Matt\xampp\htdocs\properties.php on line 33.
@Rijk, as for "Notice: Only variable references should be returned by reference", just assign result of $this->getUndefinedProperty($property) to a variable before returning it.
|
0

This is because you need to also override the __isset magic method to check if the array index exists.

2 Comments

Hm, I've just implemented an __isset() method, and it appears to not be called at all? Is this not the one used to override isset( $Object->property ) behavior?
It's like : public function __isset($key) { return isset($this->tab[$key]); } . I had this issue when doing nearly the same thing as you, and this solution made the trick.

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.