开发者

Adding a little String variable in an Array Causes Syntax Error?

开发者 https://www.devze.com 2023-02-18 21:07 出处:网络
I\'m trying to add a dash - and a defined string $pageurle right after the {target} sothat the url in the href (se below code) is extended with, naturally a dash and the contents of the variable $page

I'm trying to add a dash - and a defined string $pageurle right after the {target} sothat the url in the href (se below code) is extended with, naturally a dash and the contents of the variable $pageurle

See the existing piece of code into which I would like to inject the dash & variable right after the href:

// html for breadcrumbs
var $breadcrumb_templat开发者_运维百科es = array(
    'separator' => '→',
    'link'      => '<a href="{target}">{name}</a>',
    'wrapper'   => '<div class="breadcrumbs">{items}</div>', 
);

Now, when I add the dash and the $pageurle, dreamweaver says i'm doing something wrong and shows an eror. I must be doing something stupid here... but what? Your ideas/code/improvements/possible dives into this matter are much appreciated by me.

Adding a little String variable in an Array Causes Syntax Error?


Pretty sure it's because class property initialising statements (the var gave it away), may only contain constants or literals.

You can't perform anything procedural in the statement.

Best to do this sort of thing in your constructor.

Edit

To illustrate

class MyClass
{
    public $breadcrumb_templates = array(
        'separator' => '&rarr;',
        'link'      => '<a href="{target}">{name}</a>',
        'wrapper'   => '<div class="breadcrumbs">{items}</div>',         
    );

    public function __construct($pageurle = null)
    {
        if (null !== $pageurle) {
            $this->breadcrumb_templates['link'] =
                sprintf('<a href="{target}-%s">{name}</a>', $pageurle);
        }
    }
}


First of all, "var" was last used in PHP 4 and has no business being in PHP 5.

Second, class properties cannot run any code (such as the "." in your array()).

Here's the workaround:

class MyClass
{
    private $breadcrumb_templates;

    public function __construct()
    {
        $this->breadcrumb_templates = array(
            'separator' => '&rarr;',
            'link'      => '<a href="{target}">{name}</a>',
            'wrapper'   => '<div class="breadcrumbs">{items}</div>', 
        );
    }
}
0

精彩评论

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