An options array in a class constructor

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
VirtuosiMedia
Forum Contributor
Posts: 133
Joined: Thu Jun 12, 2008 6:16 pm

An options array in a class constructor

Post by VirtuosiMedia »

I've been working a lot with the MooTools JavaScript library lately. One of the things I've really liked about writing MooTools classes is the syntax. After spending about a solid month working exclusively on MooTools classes, when I went back to some PHP, I found I really missed the options syntax for the class constructor, so I decided to try to implement Moo-style options for PHP classes.

This isn't really anything special, but I thought I would share because someone might find it interesting. You would use an options array when you want to dictate a class' behavior at times, but at other times you want to rely on default settings. Really, it's nothing more than a glorified optional parameter, but it allows you to save some space and it's a cleaner syntax (IMO). Any thoughts?

The options class:

Code: Select all

<?php
class VMClass {
 
        // @var array $options - The generic options array for which extending classes should set default values
        public $options = array();
 
        /**
        * Description: The setOptions method should be called in the constructor of an extending class
        * @param array $options - The options array resets any default options present in the class
        * @return - $this
        */
        protected function setOptions($options) {
                if (is_array($options)){
                        foreach ($options as $key => $value){
                                $this->options[$key] = $value;
                        }
                        $this->options = $this->arrayToObject($this->options);
                }
                return $this;
        }
 
        /**
        * Description: Recursively returns an array as an object, for easier syntax
        * Credit: Mithras @ http://us2.php.net/manual/en/language.t ... .php#85237
        * @param array $array - The array to return as an object
        * @return - The object converted from the array
        */
        public function arrayToObject(array $array){
                foreach ($array as $key => $value){
                     if (is_array($value)) $array[$key] = $this->arrayToObject($value);
                }
                return (object) $array;
        }
}
 
?>
A silly test class that extends the above class:

Code: Select all

<?php
class TestClass extends VMClass {
 
        //The default options settings
        public $options = array(
                'name'          => array('first'=>'Fred',
                                               'last'=>'Flintstone'),
                'question'      =>'How are you?',
                'useQuestion' => TRUE
        );
 
        function __construct($options = null){
                $this->setOptions($options);
        }
 
        public function greet($salutation){
                echo $salutation.', '.$this->options->name->first.' '.$this->options->name->last.'. ';
                if ($this->options->useQuestion) echo $this->options->question;
        }
}
 
?>
And finally, a simple test case of our test class:

Code: Select all

<?php
include('vmclass.php');
include('testclass.php');
$options = array('question'=>'Que pasa?', 'useQuestion'=>TRUE);
$foo = new TestClass($options);
$foo->greet('Howdy');
$foo->options->name->first = 'Wilma';
$foo->options->useQuestion = FALSE;
$foo->greet('Bonjour');
?>
The above code should result in the following message echoed to the screen: "Howdy, Fred Flintstone. Que pasa?Bonjour, Wilma Flintstone."
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: An options array in a class constructor

Post by josh »

This would be good for several ( read few ) options that need to change order from time to time, as your options get hierarchical I'd want to use a configuration object that gets passed as a parameter. For instance if you used a config object you could easily have your options not only come from code ( your array ), but an ini or xml file. One such implementation is the zend framework's zend_config, granted you could still take a config object and map it to your array, its an unnecessary layer of insulation in my opinion, the config object could be considered a fundamental object to your framework and just be read directly by the classes that need configuration. Now all your classes depend on a config object, but that config object could be hidden from your applications users for instance by allowing data to come from an array, and just use the config object for passing the data around between your models and controllers
Post Reply