0

sorry for the vague title... I have a problem with a php4 class that seems hard to put to words.

My class has a "possibleValues" array, that holds information on what kind of values are acceptable for certain attributes that can be changed from the outside. In my pseudocode example below you can see that I have attributes that share the same acceptable values (colors). Obviously, the $this->colors in my code fails, because you can't define one class variable via another (can you?).

How would I go about setting a common colors array that I could reference like this so I don't have to repeat the same valid options for different fields that allow the same values?

Class MyTest {

    var $colors = array('red', 'yellow', 'blue');

    var $possibleValues = array(
        'someAttribute',               array(0,1),
        'someEmotions',                array('happy, 'sad'),
-->     'someColorableAttribute',      $this->colors,
-->     'someOtherColorableAttribute', $this->colors,
     );

    ...

}
4

2 回答 2

1

在回答您的问题时,您应该在构造函数中设置变量。在你真的不应该使用的 PHP4 中,构造函数应该具有类的名称。

function MyTest()
{
   $this->var = $this->colors;
}
于 2012-08-20T11:39:51.970 回答
1

如果您不想设置$possibleValuesin 构造函数,可以尝试这种方法:

PHP 4

class MyTest {
    var $colors = array('red', 'yellow', 'blue');

    var $possibleValues = array(
        'someAttribute' =>             array(0,1),
        'someEmotions'  =>             array('happy', 'sad'),
        'someColorableAttribute' =>    'colors',
        'someOtherColorableAttribute' => 'colors',
     );


    function getPossibleValues($attr) {
        //no attribute, empty list
        if (!array_key_exists($attr, $this->possibleValues))
            return array();

        $possible_values = $this->possibleValues[$attr];
        //value is a string, check for object variable with such name
        if (is_string($possible_values)) {
            if (!array_key_exists($possible_values, get_object_vars($this)))
                return array();

            return $this->$possible_values;
        }

        return $possible_values;
    }
}

$a = new MyTest();
var_dump($a->getPossibleValues('someAttribute'));
var_dump($a->getPossibleValues('someEmotions'));
var_dump($a->getPossibleValues('someColorableAttribute'));
var_dump($a->getPossibleValues('someOtherColorableAttribute'));

我正在使用get_object_vars,因为property_existsPHP 4 中不存在。

PHP 5(类常量)

class MyTest {
    const COLORS = 'red|yellow|blue';

    private $possibleValues = array(
        'someAttribute' =>             array(0,1),
        'someEmotions'  =>             array('happy', 'sad'),
        'someColorableAttribute' =>    self::COLORS,
        'someOtherColorableAttribute' => self::COLORS,
     );


    public function getPossibleValues($attr) {
        //no attribute, empty list
        if (!array_key_exists($attr, $this->possibleValues))
            return array();

        $possible_values = $this->possibleValues[$attr];
        //value is a string, explode it around |
        if (is_string($possible_values)) {
            return explode('|', $possible_values);
        }

        return $possible_values;
    }
}
于 2012-08-20T12:13:42.190 回答