0

Why is this a compile error:

<?php
class Bean
{
    public $text = array("123", "456");
    public $more = array("000 {$this->text[0]} 000", "--- {$this->text[1]} ---");

}

?>

The compiler says PHP Parse error: syntax error, unexpected '"'

How can I use my text array within my other arrays?

5
  • LIne 5 should be public $more = array("000 {" . $this->text[0] . "} 000", "--- {" . $this->text[1] . "} ---"); Commented Jan 23, 2015 at 9:51
  • This doesn't work. I checked ideone Commented Jan 23, 2015 at 9:54
  • I don't think that you can get this to work with current versions of php. Even though some restricitions regarding the "must be a const value"-rule have been dropped in the latest php releases.... like this? ... I don't think so. see php.net/manual/migration56.new-features.php "Constant expressions" Commented Jan 23, 2015 at 9:56
  • same concept stackoverflow.com/questions/1633012/… Commented Jan 23, 2015 at 9:57
  • you should be able to dynamically fill the array though. This is quite interresting. I want to know why this doesn't work. Commented Jan 23, 2015 at 9:57

3 Answers 3

3

As mentioned earlier you can't do that (directly) with current versions of php. Even the new features of php 5.6 won't allow that, see http://php.net/manual/en/migration56.new-features.php
But let's assume you have a valid intrest in this, like e.g. keeping/grouping something in the more declarative section of the class than hiding it somewhere in a pile of code, you could do something ( maybe a "bit" more sophisticated ;-) ) like

<?php
class Bean
{
    public $text = array("123", "456");
    public $more = array('000 %1$s 000', '--- %2$s ---');

    public function Bean() {
        foreach($this->more as $k=>&$v) {
            $v = vsprintf($v, $this->text);
        }
    }
}

$b = new Bean;
print_r($b->more);
Sign up to request clarification or add additional context in comments.

5 Comments

This "bit more sophisticated" answer is quite interesting. Where can I find documents on what the heck %1$s does?
will the vsprintf work with double quotes? @penu: search for vsprintf or sprintf in the documentation of PHP.
The format is documented under docs.php.net/sprintf at/after "The format string supports argument numbering/swapping [...]"
Neither sprintf() nor vsprintf() (nor any other function?) cares about single- or double-quoted string literals. That magic occures before the function call is made and the function itself just gets an (internal) representation of a string, no matter how that representation came into existence.
And as a side-note: I have no idea whether is a good idea or not. In a recent project I had something remotely similar...and decided to solve it via code generation (having a cumstom build step) rather than "within" the code. It has some kind of code smell on it but I can't put my finger on it right now..... just a feeling.
1

What you do in that line:

public $more = array("000 {$this->text[0]} 000", "--- {$this->text[1]} ---");

is not working in PHP.

http://php.net/manual/en/language.oop5.properties.php

here you can see valid and invalid values for properties in that example. So if you use double quotes PHP try to resolve the string.

http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double

So you have to replace your " with ' then it should work

public $more = array('000 {$this->text[0]} 000)', '(--- {$this->text[1]} ---)');

what you can do is to set a placeholder in that variable and replace them before you need them with vsprintf for example.

4 Comments

So basically I cannot use variables in these strings?
You can only set scalar values., that's not true. Array is not scalar but still you can use it.
Yes sorry it was the wrong word. I try to find the part in the documentation which describe it but i can't find it.
0

You could do this:

class Bean
{
public $text = array("123", "456");

public function fillMore () {
    $more = array();
    $more[0] = "000 ".$this->text[0]." 000";
    $more[1] = "000 ".$this->text[1]." 000";

    var_dump($more);
}
}

$bean = new Bean();
$bean->fillMore();

Alternatively you could try and fill the $more in your constructor too. This will get you your $more when you initialize the class.

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.