0

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 ?

9
  • I don't think there is a more effective way -- you've done it correctly by checking is_array() and iterating over it if it is an array, or performing the single action if not. Looks just fine to me :) Commented Nov 6, 2011 at 0:07
  • will echo $js->gimme_js_link('jquery, ...', 'libs'); ?> work for arrays too? Commented Nov 6, 2011 at 0:09
  • i need to implode the comma seperated js files before processing Commented Nov 6, 2011 at 0:11
  • You can't echo it, since it returns an array From here it looks like you've coded it correctly. Just try:print_r($js->gimme_js_link(array('jquery', 'somethingelse'), 'libs'); Commented Nov 6, 2011 at 0:12
  • In other words, you need to pass in an array, not a comma-separated list Commented Nov 6, 2011 at 0:12

5 Answers 5

1

I took out the class properties so it can be run on codepad quickly.

<?php
function gimme_js_link( $list, $folder = '')
{
    $list = explode( ',', $list);
    $links = array();
    foreach( $list as $name)
    {
        $links[] = make_js_link( trim( $name), $folder);
    }
    echo implode( '', $links);
} 

function make_js_link ($name, $folder)
{
    $dir = $folder . '/';
    return '<script src="' . $dir . $name . '.js"></script>';
}

gimme_js_link( 'jquery, functions, files', 'libs');
echo "\n";
gimme_js_link( 'jquery', 'libs');

Output:

<script src="libs/jquery.js"></script><script src="libs/functions.js"></script><script src="libs/files.js"></script>
<script src="libs/jquery.js"></script>
Sign up to request clarification or add additional context in comments.

1 Comment

can't get it work within class. Getting error message : Call to undefined function make_js_link()
1

It is usually done this way:

if(!is_array($input))
    $input = array($input);

For your string thingy you would have to code some function explicitly, that converts it into an array. I suggest to check via some regex if its a string of a list and if so, convert it into an array using a separate function.

Comments

1

It seems you have coded it rightly, just when you call the gimme_js_link() function, pass the 1st argument as an array whether you have a single list item or multiple items. Doing so you can skip the array checking part in your code. Refer the modified call to the function:

<? $js= new htmlize(); $js->gimme_js_link(array('jquery'), 'libs'); //for single list item. ?> 
<? $js= new htmlize(); $js->gimme_js_link(array('jquery','custom_script','another_script'), 'libs'); //for multiple list items ?> 

If you call this way, you can skip the array checking part in your code and process the $list variable with the foreach loop directly.

What you want is a feature called variable arguments. Where you can specify a parameter is going to accept multiple arguments. Since PHP doesn't have the variable argument feature like there is one in Java (you can refer here for a java example), array is the way to achieve this in php.

1 Comment

+1 because I like the consistency of this method the most. This how I usually handle these types of situations, I tend to avoid CSVs for some reason.
0

Although the array solution is not too bad, func_get_args() seems to be what you are looking for.

Also, please take a look at this discussion.

1 Comment

take a look at comments please
0

Something like this would do, but I would highly discourage such a pattern. It's not easy to understand what the syntax is without giving a rather large description in the docblock. Also it's just plain wrong.

<?php

class htmlize
{
    public function gimme_js_link()
    {
        if (2 > func_num_args())
        {
            // Throw an exception if argument count is incorrect
            throw new BadMethodCallException('Method needs at least one library file to load and a directory to load it from.');
        }

        $list = func_get_args();
        $dir = array_pop($list);

        foreach ($list as $item)
        {
            // Load $item from $dir
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.