MooTools options syntax in PHP classes

28 views
Skip to first unread message

VirtuosiMedia

unread,
Oct 14, 2008, 2:42:51 PM10/14/08
to MooTools Users
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 since it's inspired by MooTools, I thought I
would share because someone might find it interesting.

The options class:

<?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.types.object.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:

<?php
class TestClass extends VMClass {

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:

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

Eric

unread,
Oct 14, 2008, 11:19:01 PM10/14/08
to MooTools Users
If you want to get into some handy PHP coding, I'd recommend checking
out the Zend framework. If you're doing any sort of development
(whether CSS, Javascript, or PHP), you should be using a framework.

-Eric
>         * Credit: Mithras @http://us2.php.net/manual/en/language.types.object.php#85237

Kevin

unread,
Oct 14, 2008, 9:10:15 PM10/14/08
to mootool...@googlegroups.com
Wonderful! But I really don't think it's a good idea to mess up the PHP code with Javascript codes or variables. We must modify the php code when we do a Javascript framework upgrade.
 
My solution is that, generates the Javascript variables (only variables) using server side scripting language, and then use it in the Javascript code. So, in Javascript files, we can see the pure Javascript code. It makes debugging and upgrading much easier.
P.S. Joomla is on almost the same way to solve the problem.
 
Greetings,
Kevin

fabiomcosta

unread,
Oct 15, 2008, 3:05:23 AM10/15/08
to MooTools Users
@Kevin
i dont undestando what you are saying.
VirtuosiMedia was just doing to php what he does in mootools.
and at the time just the setOptions function.
w/e.



On Oct 14, 11:10 pm, Kevin <com...@gmail.com> wrote:
> Wonderful! But I really don't think it's a good idea to mess up the PHP code
> with Javascript codes or variables. We must modify the php code when we do a
> Javascript framework upgrade.
>
> My solution is that, generates the Javascript variables (only variables)
> using server side scripting language, and then use it in the Javascript
> code. So, in Javascript files, we can see the pure Javascript code. It makes
> debugging and upgrading much easier.
> P.S. Joomla is on almost the same way to solve the problem.
>
> Greetings,
> Kevin
Reply all
Reply to author
Forward
0 new messages