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!
Properties::setProtectedProperty($var)?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.