Sam
Sam

Reputation: 15508

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

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_templates = 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.

enter image description here

Upvotes: 3

Views: 301

Answers (2)

Theodore R. Smith
Theodore R. Smith

Reputation: 23259

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>', 
        );
    }
}

Upvotes: -1

Phil
Phil

Reputation: 164895

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);
        }
    }
}

Upvotes: 3

Related Questions