20

I have a problem with TWIG. This code works at school but absolutely not with my laptop. I tried with a simple code but I have the error:

Catchable fatal error: Argument 1 passed to Twig_Filter::__construct() must be an instance of string, string given, called in /opt/lampp/htdocs/webalizer/projetSilex/vendor/twig/twig/lib/Twig/Extension/Core.php on line 139 and defined in /opt/lampp/htdocs/webalizer/projetSilex/vendor/twig/twig/lib/Twig/Filter.php on line 35

I use php 5.6 / SILEX 2.0 / Twig 2.0

Thanks for your help.

My code is very simple but doesn't work:

require_once __DIR__.'/vendor/autoload.php';

$app = new Silex\Application();
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/views',
  ));

  $app->get('/', function(){
return "hello";
  });

 $app->get('/hello/{name}', function($name)use($app){

 return $app['twig']->render('hello.twig',
                            array("name"=>$name
                                ));

 });

 $app->run();>
wogsland
  • 9,106
  • 19
  • 57
  • 93
Ben
  • 203
  • 1
  • 2
  • 5
  • 3
    You cannot type declare scalar types in PHP5. https://secure.php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration – Charlotte Dunois Jan 26 '17 at 18:09
  • 2
    you must pass a string, not a string, this is quite clear ;-) (composer should warns you of installing Twig 2.0 with Php 5.6 no?) – Thomas Decaux Jan 28 '17 at 10:29
  • Possible duplicate of [Error when passing string into method with type hinting](https://stackoverflow.com/questions/3112791/error-when-passing-string-into-method-with-type-hinting) – Axel Oct 13 '17 at 10:37

2 Answers2

32

@CharlotteDunois pointed this out, but Twig 2.0 requires >=PHP7.0, so in your environment (php 5.6) you can't use Twig 2.0. From the Twig official documentation:

Prerequisites

Twig needs at least PHP 7.0.0 to run.

Notice that for PHP5.x branch you still have Twig 1.x aviable

mTorres
  • 3,590
  • 2
  • 25
  • 36
0

just my 2 cents. I couldn't upgrade PHP version (shared hosting), so I had to change Twig version in my package.json to use 1.x version.

Replace this line: "symfony/twig-bridge": "~2.8|^3.0", -> "symfony/twig-bridge": "~2.8|3.0.*",

then, I added this line:

"twig/twig" : "~1.0"

and evething worked properly on PHP server v 5.6