PHP Dice IoC - Parse error: syntax error, unexpected '.'?

350 Views Asked by At

I am trying to use Dice for my dependency injection.

When I run the code below (from Dice doc),

include 'dice.php';

class A {
    private $b;

    public function __construct(B $b) {
        $this->b = $b;
    }
}

class B {
    private $c,$d;

    public function __construct(C $c, D $d) {
        $this->c = $c;
        $this->d = $d;
    }
}

class C {

}

class D {
    private $e;

    public function __construct(E $e) {
        $this->e = $e;
    }
}

class E {

}


$dice = new \Dice\Dice;
$a = $dice->create('A');
print_r($a);

I get this error,

Parse error: syntax error, unexpected '.' in C:...\dice.php on line 38

If you open dice.php, this is the line the error message refers to,

else $object = $params ? new $class->name(...$params($args)) : new $class->name;

I does look strange to me - ...$params what is that??

Any ideas?

2

There are 2 best solutions below

4
On BEST ANSWER

The vararg/splat operator (...) is a new feature introduced in PHP 5.6. Strange that a popular library relies on such obscure syntax but it seems you have to upgrade for your code to successfully run.

PHP 5.6 New features

PHP 5.6 and the Splat Operator

0
On

PHP 5.4-5.5 version isn't available anymore on https://github.com/TomBZombie/Dice. Most of shared hosting still use 5.5. Can anybody set up to be accessible from sdome link. I know it's a little violation of rules. Thank You!