0

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 .. Are all right I want to do? sorry for bad english

1 Answer 1

1

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 ([email protected])
 * @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;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.