Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP OOP链接生成器?_Php_Hyperlink_Helper - Fatal编程技术网

PHP OOP链接生成器?

PHP OOP链接生成器?,php,hyperlink,helper,Php,Hyperlink,Helper,我用php编写了一个类,我需要在该类的函数中编写的所有引用都有一个前缀。那就是 class MyClass { function echoing() { $class = new LinkMaker(); return $class->create(array('href' => 'popular/books/', 'title' => 'Popular books of 2010')); } } 然后在LinkMake

我用php编写了一个类,我需要在该类的函数中编写的所有引用都有一个前缀。那就是

class MyClass
{
    function echoing()
    {
        $class = new LinkMaker();
        return $class->create(array('href' => 'popular/books/', 'title' => 'Popular books of 2010'));
    }
}
然后在LinkMaker类中,我为链接添加了前缀。。我想怎么办?
对不起,英语不好

我想我明白你的意思了。您希望在服务器端构建DOM

下面是我编写的重载PHP5类,它实现了这一点。它将接受任何HTML5锚属性

用法:

<?
    $links = array(
        new BabyLink(array('href' => '/home', 'name' => 'home', 'title' => 'Home Page', 'label' => 'Home')),
        new BabyLink(array('href' => '/about', 'name' => 'account', 'title' => 'About This Site', 'label' => 'About')),
        new BabyLink(array('href' => '/contact', 'name' => 'contact', 'title' => 'How To Contact Us', 'label' => 'Contact')),
        new BabyLink(array('href' => '/logout', 'name' => 'logout', 'title' => 'Log Out', 'label' => 'Logout'))
        );

foreach ($links as $link)
     {
     $link->render();
     }

<?

/**
 * BabyLink HTML5 Anchor Link Model
 * @author Warren Stevens (warbaby67@gmail.com)
 * @package Baby
 **/

class BabyLink
    {

    public $id = false;

    protected $me = array();

    public $fields = array('id', 'accesskey', 'class','contenteditable', 'contextmenu', 'data-', 'draggable', 'hidden', 'href', 'hreflang', 'item', 'itemprop', 'label', 'lang', 'media', 'ping', 'rel', 'spellcheck', 'style', 'subject', 'tabindex', 'target', 'title', 'type');

    function __construct(array $a) { $this->set($a); }

    /**
     * @param string
     * @param array
     **/
    function __call($k, $args = array()) { return $this->me[$k]; }

    function get() { return $this->me; }

    function set(array $a)
        {
        foreach($this->fields as $k) { if(isset($a[$k])) { $this->me[$k] = $a[$k];}}
        $this->id = $this->me['id'];
        }

##### PUBLIC


    public function render()
        {
        $str = '<a ';
        foreach ($this->me as $k => $v)
            {
            $str .= $k.'="'.$v.'" ';
            }
        $str .= '>'.$this->label().'</a>';
        print $str;
        }
    }