Two simple designs, which is better?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply

Which is better?

ChoiceA
0
No votes
ChoiceB
1
100%
Neither, I'll comment below
0
No votes
 
Total votes: 1

User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Two simple designs, which is better?

Post by Ollie Saunders »

The actual functionality here is really trivial. I'm pretty sold on ChoiceB now. But having prepared this post, here it is anyway.

What both of these do is to provide an interface to an array of classes. Classes as in the HTML attribute:

Code: Select all

<div class="">
^ that kind of class, a CSS class.
You can, add, remove and parse from a string. Please note the code itself is untested.

ChoiceA

Code: Select all

class ChoiceA
{
    private $_classes;
    public function __get($name)
    {
        if ($name == 'class') {
            return implode(' ', $this->_classes);
        }
    }
    public function __set($name, $value)
    {
        if ($name == 'class') {
            $this->_classes = preg_split('/\s+/', trim($value));
        }
    }
    public function addClass($toAdd)
    {
        $this->_classes[] = removeAllWhitespace($toAdd);
    }
    public function removeClass($toAdd)
    {
        $result = array_search($toAdd, $this->_classes);
        if ($result !== false) {
            $this->_classes[$result] = null;
        }
    }
}
/**** USAGE ******/
$div = new ChoiceA;
$div->class = 'foo bar';
$div->addClass('zim');
// $div->class == 'foo bar zim';
$div->removeClass('bar');
// $div->class == 'foo zim';
ChoiceB

Code: Select all

class ChoiceB
{
    private $_classes;
    public function __construct()
    {
        $this->_classes = new HtmlClassManager;
    }
    public function __get($name)
    {
        if ($name == 'class') {
            return $this->_classes;
        }
    }
    // no __set
}
class HtmlClassManager
{
    private $_classes;
    public function add($class)
    {
        $this->_classes[] = $class;
    }
    public function remove($class)
    {
        $result = array_search($toAdd, $this->_classes);
        if ($result !== false) {
            $this->_classes[$result] = null;
        }
    }
    public function set($classes) // just an alias
    {
        $this->parseFromString($classes);
    }
    public function parseFromString($classes)
    {
        $this->_classes = preg_split('/\s+/', trim($classes));
    }
    public function get()
    {
        return implode(' ', $this->_classes);
    }
}
/**** USAGE ******/
$div = new ChoiceB;
$div->class->parseFromString('foo bar');
$div->class->add('zim');
$div->class->remove('bar');
// $div->class->get() == 'foo zim';
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The "B" path feels better from a quick once-over. It feels more compartmentalized, more object oriented.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I agree with feyd. It looks a little more resusable.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Yep, good good.
Post Reply