开发者

PHP OOP link maker?

开发者 https://www.devze.com 2023-01-24 09:04 出处:网络
I write a class in php and I need that all references that I am writing in the functions of the class had a prefix. That is...

I write a class in php and I need that all references that I am writing in the functions of the class had a prefix. That is...

class MyClass
{
    function echoing()
    {
        $class = new LinkMaker();
        return $class->create(array('href' => 'popular/books/', 'title' => 'Popular books of 2010'));
    }
}

And then in the class LinkMaker I add a prefix to the links开发者_JAVA技巧 .. Are all right I want to do? sorry for bad english


I think I understand what you are saying. You want to kindof build the DOM on the server side.

Below is an Overloaded PHP 5 class I wrote which does this. It will take any HTML 5 anchor attribute.

Usage:

<?
    $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;
        }
    }
0

精彩评论

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