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?

penu
  • 968
  • 1
  • 9
  • 22
  • LIne 5 should be `public $more = array("000 {" . $this->text[0] . "} 000", "--- {" . $this->text[1] . "} ---");` – Hussain Jan 23 '15 at 09:51
  • This doesn't work. I checked ideone – penu Jan 23 '15 at 09: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 http://php.net/manual/migration56.new-features.php "Constant expressions" – VolkerK Jan 23 '15 at 09:56
  • same concept http://stackoverflow.com/questions/1633012/initialize-class-property-with-an-anonymous-function – Kevin Jan 23 '15 at 09:57
  • you should be able to dynamically fill the array though. This is quite interresting. I want to know why this doesn't work. – Tikkes Jan 23 '15 at 09:57

3 Answers3

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);
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • This "bit more sophisticated" answer is quite interesting. Where can I find documents on what the heck %1$s does? – penu Jan 23 '15 at 10:09
  • will the `vsprintf` work with double quotes? @penu: search for `vsprintf` or `sprintf` in the documentation of PHP. – Tikkes Jan 23 '15 at 10:12
  • The format is documented under http://docs.php.net/sprintf at/after "The format string supports argument numbering/swapping [...]" – VolkerK Jan 23 '15 at 10:15
  • 1
    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. – VolkerK Jan 23 '15 at 10:16
  • 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. – VolkerK Jan 23 '15 at 10:25
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.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
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.

Tikkes
  • 4,599
  • 4
  • 36
  • 62