Вопрос

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,
     );

    ...

}
Это было полезно?

Решение

In answer to your question, you should set the variable in the constructor. In PHP4, which you really shouldn't be using, the constructor should have the name of the class.

function MyTest()
{
   $this->var = $this->colors;
}

Другие советы

If you don't want to set the $possibleValues in constructor, you can try this approach:

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'));

I'm using get_object_vars since property_exists don't exist in PHP 4.

PHP 5 (class constant)

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;
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top