Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7891161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:32:51+00:00 2026-06-03T06:32:51+00:00

I have a PHP class for building HTML tags. Each HTML tag becomes of

  • 0

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:32:52+00:00Added an answer on June 3, 2026 at 6:32 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have certain PHP class methods that access external variables. These variables are not
I have an external file with some vars to require in my php class
Im currently building a php framework... again. I have a class called config. its
If I create a new PHP class e.g. to simplify form building (yes I
I'm building some classes for working with playing cards. I have a Card class
I have a PHP site I'm building and I seem to be having some
I'm building up a library of filters for a validation class in PHP, some
I have a PHP class that stores a complex multidimensional array, and rather than
I have a php class that generates a map image depending on my db
I have a PHP class with a method. In the base class (it's more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.