Handling templates for a web application I am writing up a class to handle various templates to be used in a web application. Each template contains various placeholders which will need to be replaced at the time of the build. I am wondering if the method I am using is the best solution, or if I should consider something else. At the moment I have two separate functions addPlaceholder() and addMultiplePlaceholders(). Each performs as their names suggest.

The function addPlaceholder() has two parameters one, the key $k, must be a string while the other, the value $v, can be anything. These get stored in the classes placeholder array. 

The function addMultiplePlaceholders() has only one parameter, $array, and it should be an associative array. This function calls addPlaceholder() for each index of $array to store the corresponding values. The reason for the call to an existing function is to help alleviate duplicitous code, and when only a single placeholder is in use to bypass the foreach loop.

Is this the best method to accomplish this? Would it be better to forgo addPlaceholder() and just use addMultiplePlaceholders() for everything? Perhaps, overloading addPlaceholder() would be a better solution having one version which accepts the two parameters, and another which accepted the array. However, I would be in the same situation as now having to decide whether to duplicate the code for adding the placeholder to the class property, or calling the other function.

public function addPlaceholder($k,$v){
    if(empty($k)){
        throw new Exception('Class Template-&gt;addPlaceholder() '.'The placeholder string cannot be empty.');
    }

    if(isset($this-&gt;placeholders[$k])){
        throw new Exception('Class Template-&gt;addPlaceholder() '.'['.$k.'] is a duplicate entry');
    }

    $this-&gt;placeholders[$k] = $v;
}

public function addMultiplePlaceholders($array){
    if(!is_associative($array)){                    // if `$array` is not an associative array throw exception
        throw new Exception("The supplied parameter value is not an associative array");
    }

    foreach($array as $key =&gt; $value){
        try{
            addPlaceholder($key,$value);
        }catch(Exception $e){
            $msg = $e-&gt;getMessage();
            preg_replace(preg_quote('Class Template-&gt;'), '', $msg);
            throw new Exception("Class Template-&gt;addMultiplePlacerholders()-&gt;".$msg);
        }
    }
}
To answer just the question in the title;
Your solution seems fine to me; addMultiplePlaceholders() is a utility method that acts as a wrapper for addPlaceholder()

Note there's a bug in your code; addMultiplePlaceholders() calls addPlaceholder() without $this-&gt; 'prefix', so your code won't work

However, there is room for improvement. I'll try to summarize what I'd do differently.

Remove redundant information from your Exception messages

Exceptions are 'smart' messages in that they already contain all the meta-information you need to find where they occurred (e.g. line number, file etc). Don't clutter your messages with that information, It'll only complicate your code (for example the preg_replace). Just include the bare error-message. See the documentation on Exceptions.

If your going to output a message, make sure they cannot be mis-interpreted. Being consistent in your messages will prevent confusion and make it easier to debug your code later on, for example:

throw new Exception('The placeholder string cannot be empty');


Is confusing; What is the placeholder string? The name ($k)? Value ($v)? 

Outputting the meta-information can be done when printing the exception, for example;

Just use this to throw your exception;

throw new Exception('Placeholder name cannot be empty');


And output it in your Exception-handler with this;

printf(
    "An Exception has occurred: '%s' in file '%s' on line '%s',
    $e-&gt;getMessage(),
    $e-&gt;getFile(),
    $e-&gt;getLine()
);

// --&gt; An Exception has occurred: 'The placeholder string cannot be empty' in file 'Template.php' on line '52'


Since you're no longer including the name of the Class/Method in your messages, you can completely remove the 'try/catch' from your 'addMultiplePlaceholders'.

Use the right 'type' of Exception

To be more expressive/specific when throwing Exceptions, consider using special Exceptions for a situation, for example, the SPL Exceptions. In this case an InvalidArgumentException will be appropriate

Consider what really is an exception

Inside addPlaceholder, you're throwing an exception if a key is already set. Personally, I wouldn't do this (it does feel like 'business logic'). Technically, overwriting the value of a placeholder that was already set won't cause any problem. Disallowing overwriting existing placeholders may limit you in situations that you want to overwrite a placeholder.

However, renaming addPlaceholder to setPlaceholder may be more appropriate, to indicate that you can both 'add' or 'update/replace' a placeholder.

Use descriptive variable/argument-names

The arguments for addPlaceholder are not very descriptive. Additionally, the variable names you're using seem to be picked based on the the 'inner workings' of the method (key/value pairs), which should be of no interest when using that method. I'd suggest something more describing the purpose of the arguments, for example:

public function addPlaceholder($name, $value)
{
    //...
}


Likewise, replace '$array' to something more describing its purpose, for example

public function addMultiplePlaceholders($nameValuePairs)
{
    //...
}

// or..

public function addMultiplePlaceholders($placeholders)
{
    //...
}


If you want to explicitly indicate you're expecting an array (and have PHP check this for you), you can add a type-hint, like this:

public function addMultiplePlaceholders(array $placeholders)
{
    //...
}


Finally, naming this method addMultiplePlaceholders may be a bit verbose. Since the method is plural, it's probably already clear that it is used to add multiple placeholders, so simply calling it addPlaceholders may be clear enough.

Remove 'comment' clutter. Add PhpDoc in stead

Do not describe code that is self-explanatory. In general, try to omit inline comments in all cases, unless a piece of code really is confusing.

In most cases, don't describe the code, but describe the purpose / intent of a function/method inside a PhpDoc comment.

For example:

if(!is_associative($array)){ // if `$array` is not an associative array throw exception
    throw new Exception("The supplied parameter value is not an associative array");
}


This comment is just repeating the code! Also, inline code is not used by IDE's and will not be included in automatically generated documentation (e.g. phpdocumentor). It's best to move relevant information to a PhpDoc block

For example:

/**
 * Adds placeholder(s) passed as an associative array ('name' =&gt; value)
 *
 * Name should be a string, value can be any type of value
 *
 * This method is a wrapper for @see addPlaceholder()
 *
 * @param array $nameValuePairs
 *
 * @throws InvalidArgumentException if $nameValuePairs is not an associative array 
 *                                  or an empty name is used for a placeholder
 * @return void
 */
public function addMultiplePlaceholders(array $nameValuePairs)
{
    //...
}


Modified code

This is the code after applying my suggestions

/**
 * Adds or updates a placeholder
 *
 * @param string $name  Name of the placeholder. Cannot be empty
 * @param mixed  $value Value of the placeholder
 *
 * @throws InvalidArgumentException if $name is invalid (empty)
 *
 * @return void
 */
public function setPlaceholder($name, $value)
{
    if (empty($name)) {
        throw new InvalidArgumentException('Placeholder $name cannot be empty.');
    }

    $this-&gt;placeholders[$name] = $value;
}

/**
 * Adds or updates placeholder(s), passed as an associative array ('name' =&gt; value)
 *
 * Name should be a string, value can be any type of value
 *
 * This method is a wrapper for @see addPlaceholder()
 *
 * @param array $placeholders placeholder(s), passed as an associative array ('name' =&gt; value)
 *
 * @throws InvalidArgumentException if $placeholders is not an associative array 
 *                                  or an empty name is used for a placeholder
 * @return void
 */
public function setPlaceholders(array $placeholders)
{
    if (!is_associative($nameValuePairs)) { 
        throw new InvalidArgumentException('Parameter $placeholders is not an associative array');
    }

    foreach (placeholders as $name =&gt; $value) {
        $this-&gt;addPlaceholder($name, $value);
    }
}