3

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

6
  • 'link' => '<a href="{target}' . " " . $pageurle . '">{name}</a>', @pst after . its still no good... error remains in DW :( thanks though for your coment +1 see my updated image Commented Mar 19, 2011 at 2:51
  • @pst You should post that as an answer Commented Mar 19, 2011 at 2:51
  • @Sam: What does PHP say when you try to actually run it? Commented Mar 19, 2011 at 2:54
  • @Phil, but that does not solve the riddle! DW still recognizes an error in that line... Although I am so desparate I would give any suggestion as an answer or not a +1 upvote right now! Commented Mar 19, 2011 at 2:54
  • 1
    @Sam: Stop staring at your editor and try running it already. Commented Mar 19, 2011 at 2:56

2 Answers 2

3

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Also, var, seriously? PHP 4 is dead and buried. Hell, they've even stopped development on 5.2
With what should the var be replaced? Thanks @Phil.
@Sam A property visibility declaration
-1 I had to turn my +1 into a -1. I mean... Why do it that way?
@Theodore It's just an example to illustrate; a) You can use an array of literal values in a property initialising statement, and b) How to manipulate properties in the constructor. It's also an example of starting with a sane default and adding data as more parameters come in. What exactly do you find "not useful" about this answer?
|
-1

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

4 Comments

+1 thanks very much @Theodore, so now how do I add the - and @pageurle right after the {target} in there? or would you propose what @Phil said: focus on the constructor and fix stuff right there?
They most certainly can be initialised with an array, as long as each key/value is a constant or literal
How does this answer the question?
@Phil Brown, unless he changed his answer, he has the need to append a variable to the array at object instantiation. I showed him how to do that.

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.