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

    ...

}
kontur
  • 4,934
  • 2
  • 36
  • 62

2 Answers2

1

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;
}
Lusitanian
  • 11,012
  • 1
  • 41
  • 38
  • That would work, yes. Is there a different way to do it in php5? I feel my idea of referencing one class variable to another is the real problem, not what version I am using. Is there other approaches to this problem? Is there a difference to class constants, for example, in php5? – kontur Aug 20 '12 at 11:50
  • Well, honestly; the standard would be to use a constructor (: – Lusitanian Aug 20 '12 at 12:48
1

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;
    }
}
Furgas
  • 2,729
  • 1
  • 18
  • 27
  • Okay, so basically I use a stand-in string to access a class variable of same name? I suppose this works, but it certainly feels like a big work around. – kontur Aug 20 '12 at 12:19
  • @kontur The standard would be to use constructor or repeat yourself (both PHP 4 and 5). I've updated the answer with PHP 5 example using class constant. – Furgas Aug 20 '12 at 12:30
  • Okay, thanks for the update. I've just seen other examples combining `define` and `serialize` for having an "constant array"/"class variable" array - interesting. Good to know my options in php5 also. Really appreciate your effort in explaining - @Lusitanian's answer seems to fit my problem better, but your's definately taught me more ;) – kontur Aug 20 '12 at 13:39