PHP combined static and non-static class vs 2 separate classes

716 Views Asked by At

I have a PHP class for building HTML tags. Each HTML tag becomes of new instance. I have some utility methods needed within the class for handling certain functional stuff like escaping attributes and for configuring certain options that affect all instances. I'm declaring the utilities and public static and within the class I'm using self:: to call the utility methods. Do you think it's better to keep all the methods in one class or to create a separate static class for the utilities and have it so the main class extends the static one (so that I can still call the methods with self::)? What is the best way to handle this type of situation? (in PHP 5.2 and up) The interface for the class is like this:

foo('div')->attr('id', 'example')->html('this is inner text')->get();

foo('img')->attr(array('src' => 'example.png', 'alt' => 'example'))->get();

Example of the underlying code:

public function attr($name, $value = '')
{
    if (is_scalar($name))
    {
        if (empty($name) && !is_int($name)) $this->attrs[] = $value; 
        else $this->attrs[$name] = $value;
    }
    else
    {
        foreach ((array) $name as $k => $v)
        {
            $this->attr($k, $v);
        }
    }
    return $this;
}

// handler (doesn't get called until it's time to get)
public static function attr_encode($name, $value = '')
{
    if (is_scalar($name))
    {
        if (is_int($name)) return self::attr_encode($value, '');
        $name = preg_replace(self::ATTR_NAME_INVALIDS, '', $name);
        if ('' === $name)  return '';
        if (1 === func_num_args() || '' === $value) return $name;

        $value = is_null($value) ? 'null' : ( !is_scalar($value) ? (
                 self::supports('ssv', $name) ? self::ssv_implode($value) : json_encode($value)) : (
                 is_bool($value) ? (true === $value ? 'true' : 'false') : self::esc_attr($value)));

         return $name . "='" . $value . "'";     // Use single quotes for compatibility with JSON.
    }

    // Non-scalar - treat $name as key/value map:
    $array = array();
    foreach ((array) $name as $attr_name => $attr_value)
    {
        self::push($array, true, self::attr_encode($attr_name, $attr_value));
    }
    return implode(' ', $array);
}
3

There are 3 best solutions below

3
Wouter Dorgelo On BEST ANSWER

This topic gives you some good info about how, when and when not to use static classes:

When to use static vs instantiated classes

Also.. You wrote:

The interface for the class is like this:

Interfaces in PHP / OOP are something else. Interfaces force the programmer to use certain methods in classes.

1
Cory Carson On

It sounds like you're building something similar to LINQ to XML classes: http://broadcast.oreilly.com/2010/10/understanding-c-simple-linq-to.html#example_1 or http://phphaml.sourceforge.net/

Perhaps theres some interface styles there you can adapt.

0
Marin Sagovac On

I'm building own PHP5 HMVC project where using on some library using STATIC with namespaces:

\Project\library\output::set_header_status(100);

But imagine your problem:

  • I want add functions before calling some functions, or avoid as:

    $output = \Project\library\output->log(true, 'directory_log_of_header/');
    
    \Project\library\output::set_header_status(100);
    

So, there was problem. This examples don't work. Instead of this example function I will avoid this using STATIC, and use $this and calling a new instance.

If I need add some function, put some variables and prepare for calling function It should be using non-static using instance.


I'm using STATIC method only for directly calling function without previous prepare function, set variables, or for counters, or setting a variable which really needs using static. For example it can be for calling GET/POST data or directly example calling CLEARING CACHE (example remove cache from files directly without non-static).

  • If I'm using only static, I don't know how many instances are calling for instances, if you have in class where calling another instances needs than better solution is really using non-static.

    People who builds projects using STATIC method you can't properly analyze where are static methods/class/function called. This would be harder for maintenance and development for the future business.

    Use STATIC only if functions offers small tasks, directly grabbing a data which didn't change often, or for small tasks as variables, dummy data (example: versions, checking, counters, checkers), for HASH algoritms and similar.