1

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);
}
ryanve
  • 50,076
  • 30
  • 102
  • 137
  • 1
    Placing all code in 1 class is not OOP. Can you show some code in which you are not sure what to do? – Wouter Dorgelo May 04 '12 at 19:39
  • @Mr.Pallazzo Example added. If the utilities are specific to the `Foo` class then don't they belong with the `Foo` class? How is that not OOP? – ryanve May 04 '12 at 19:44
  • Static methods should only be used sparingly. Excessive use of static is a code smell. Just imagine that the word "static" is replaced with "global" and you ought to realise why. – GordonM May 04 '12 at 19:45
  • _Each HTML tag becomes of new instance._ - If you mean what I think you mean, your functions should be returning `$this`, not creating a new instance. – AndrewR May 04 '12 at 19:51
  • @AndrewR They are—look in my example above. – ryanve May 04 '12 at 19:53
  • @GordonM Why compare static to global? – ryanve May 04 '12 at 20:25
  • I'd also love to get your guys' input on another PHP question here: http://stackoverflow.com/questions/10442668/php-methods-that-work-in-both-instantiated-and-static-contexts – ryanve May 04 '12 at 20:28

3 Answers3

1

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.

Community
  • 1
  • 1
Wouter Dorgelo
  • 11,770
  • 11
  • 62
  • 80
  • I have no doubt that I need some static methods. What I'm trying to decide is whether it's better to include them in the main class or create a separate class that is purely static and extend the main class from that. – ryanve May 04 '12 at 20:06
  • 1
    Create a seperate class that is purely static :-) In that case it's easier to use that static code elsewhere if needed – Wouter Dorgelo May 04 '12 at 20:08
  • Got em' separated now and yea I think it's gonna be better. I have it so the non-static one extends the static one, so that I can use `self::` to call the utils from there. – ryanve May 17 '12 at 17:06
1

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.

Cory Carson
  • 276
  • 1
  • 6
  • Cool - thanks - good to know about. What I'm doing is a bit simpler. I'll post the link when I have it Github. – ryanve May 08 '12 at 15:25
0

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.

Marin Sagovac
  • 3,932
  • 5
  • 23
  • 53