My PHP class looks like that.
<?php
class htmlize {
protected $base_dir;
public function __construct( $base_dir = 'core/code/js/' )
{
$this->base_dir = $base_dir;
}
public function gimme_js_link ($list, $folder = '')
{
if(is_array($list)) {
$links=array();
foreach ($list as $name)
{
$links[]=$this->make_js_link ($name, $folder);
}
print_r ($links);
}
else {echo $this->make_js_link ($list, $folder);}
}
protected function make_js_link ($name, $folder)
{
$dir = $this->base_dir . $folder . '/';
return '<script src="' . $dir . $name . '.js"></script>';
}
}
?>
Calling it like that
<? $js= new htmlize(); $js->gimme_js_link('jquery', 'libs'); ?>
The question is how can i get work this class either for array like 'jquery, ... , ...' or for single element for ex 'jquery'? Currently I made changes to gimme_js_link method: it checks $listvariable whether it's array or not before processing.
But i feel that there must be more effective way, and we can shorten the code. Any suggestions ?
is_array()and iterating over it if it is an array, or performing the single action if not. Looks just fine to me :)echo $js->gimme_js_link('jquery, ...', 'libs'); ?>work for arrays too?print_r($js->gimme_js_link(array('jquery', 'somethingelse'), 'libs');