Search code examples
phpdependency-injectioninversion-of-controldicephp-5.5

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


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?


Solution

  • 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