Jason Van Malder Jason Van Malder - 1 year ago 79
PHP Question

PHP - function which initializes variables

I would like to make cross variables in my template via the router.

I want to do:


Here is my with function:

public function with($vars)
foreach ($vars as $key => $value)
$$key = $value;
die("La fonction with() demande un tableau en param├Ętre.");

Am I on the right track?

Answer Source

You can instead just extract your keys as vars in your template. Like this

function with($view, array $data = []) {
    require $view . '.php';

with('some_view', [
    'name' => 'John Doe'

Then you can use it in your view, like so

<h1><?= $name ?></h1>