views:

8

answers:

1

Hi,

I have recently been implementing a templating system for my home-grown web-site generator platform. What I'm currently trying to implement is the use of an interface throughout the template class heirarchy like so (this is just an example):

interface IStyleSheet
{
    public function StyleSheetFragment();
}

abstract class TemplateBase
{
    public abstract function DoRender();
}

abstract class TemplateRoot extends TemplateBase implements IStyleSheet
{
    public function DoRender()
    {
        ?>
        <div id='content1'>
            <?php 
                $this->Content1();
            ?>
        </div>
        <div id='content2'>
            <?php 
                $this->Content2();
            ?>
        </div>
        <?php 
    }

    public abstract function Content1();
    public abstract function Content2();

    public function StyleSheetFragment()
    {
        ?>
            <style>
                #content1{
                    text-align: center;
                }

                #content2{
                    text-align: right;
                }
            </style>
        <?php 
    }
}

class PageView_Home extends TemplateRoot implements IStyleSheet
{
    public function Content1()
    {
        ?>
            <div id='foo'>
                <?php 
                    // some ui stuff here
                ?>
            </div>
        <?php 
    }

    public function Content2()
    {
        ?>
            <div id='bar'>
                <?php 
                    // some more ui stuff here
                ?>
            </div>
        <?php 
    }

    public function StyleSheetFragment()
    {
        ?>
        <style>
            #foo{
            /*
                styling stuff
            */
            }

            #bar{
            /*
                more styling stuff 
*/
            }
        </style>
        <?php 
    }
}

now, the view class at the end of the heirarchy is called upon using reflection, what I want to be able to do is cycle through the list of base classes 1 by 1 and call the interface function StyleSheetFragment() in each one without it automatically resolving to the one overridden version in the view class.

The other option I was considering was to use static functions but that cannot be enforced with an interface, I could define it in the very base class but again, in that instance there is nothing that will specifically denote the class as being self-styled (so to speak).

It really needs to be enforced by type, hence my choosing to use the interface pattern on it.

Any comments or suggestions would be most appreciated.

A: 

Turns out that static functions can actually go in php interfaces so ive used that as a solution.