开发者

trying to call an interface function throughout an abstract hierarchy of an object instance

开发者 https://www.devze.com 2023-01-13 03:08 出处:网络
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 hie

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 hierarchy 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开发者_JAVA百科
{
    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 hierarchy 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消