tags:

views:

1010

answers:

3

Does PHP have a method of having auto-generated class variables? I think I've seen something like this before but I'm not certain.

public class TestClass {
    private $data = array();

    public function TestClass() {
        $this->data['firstValue'] = "cheese";
    }
}

The $this->data array is always an associative array but they keys change from class to class. Is there any viable way to access $this->data['firstValue'] from $this->firstValue without having to define the link?

And if it is, are there any downsides to it?

Or is there a static method of defining the link in a way which won't explode if the $this->data array doesn't contain that key?

+10  A: 

See here: http://www.php.net/manual/en/language.oop5.overloading.php

What you want is the "__get" method. There is an example for what you need on the link.

Jan Hancic
A: 

For associative arrays you can use the function "isset()" to see if the array contains a value on that particular key.

if(isset($this->data['firstValue'])) {
    // do anything with data['firstValue']
}
Johnny
+1  A: 

Use the PHP5 "magic" __get() method. It would work like so:

public class TestClass {
    private $data = array();

    // Since you're using PHP5, you should be using PHP5 style constructors.
    public function __construct() {
        $this->data['firstValue'] = "cheese";
    }

    /**
     * This is the magic get function.  Any class variable you try to access from 
     * outside the class that is not public will go through this method.  The variable
     * name will be passed in to the $param parameter.  For this example, all 
     * will be retrieved from the private $data array.  If the variable doesn't exist
     * in the array, then the method will return null.
     *
     * @param string $param Class variable name
     *
     * @return mixed
     */
    public function __get($param) {
        if (isset($this->data[$param])) {
            return $this->data[$param];
        } else {
            return null;
        }
    }

    /**
     * This is the "magic" isset method.  It is very important to implement this 
     * method when using __get to change or retrieve data members from private or 
     * protected members.  If it is not implemented, code that checks to see if a
     * particular variable has been set will fail even though you'll be able to 
     * retrieve a value for that variable.
     *
     * @param string $param Variable name to check
     * 
     * @return boolean
     */
    public function __isset($param) {
        return isset($this->data[$param]);
    }

    /**
     * This method is required if you want to be able to set variables from outside
     * your class without providing explicit setter options.  Similar to accessing
     * a variable using $foo = $object->firstValue, this method allows you to set 
     * the value of a variable (any variable in this case, but it can be limited 
     * by modifying this method) by doing something like:
     * $this->secondValue = 'foo';
     * 
     * @param string $param Class variable name to set
     * @param mixed  $value Value to set
     * 
     * @return null
     */
    public function __isset($param, $value) {
        $this->data[$param] = $value;
    }
}

Using the magic __get, __set, and __isset constructors will allow you to control how you want variables to be set on a class while still storing all the values in a single array.

Hope this helps :)

David Stockton
Great answer. I would vote it up if I had any votes left today!
Philip Morton