Because return is limited to only one value.
By extracting it you are taking one value the array and splitting it into many.
Personally I avoid using things like extract and $$var in my code as it destroys my IDE and make readability near impossible. That said, in this case it does make sense to use it, because it's within a limited scope which limits the possibility of accidentally overriding another variable unintentionally.
http://php.net/manual/en/functions.returning-values.php
A function can not return multiple values, but similar results can be obtained by returning an array.
AND
http://php.net/manual/en/function.extract.php
extract — Import variables into the current symbol table from an array
symbol table ~ scope
Further
Return Values: Returns the number of variables successfully imported
into the symbol table
public function view($path, $data)
{
$dir = $_SERVER['DOCUMENT_ROOT'].SITE_NAME."/";
include($dir.$path.".php");
return extract($data); //returns the variables successfully imported from $data
}
.
As you can see once you call return, that ends the execution of the current function, and closes that scope. You will have to re-arrange these, so the variable assignment takes place first.
I'm assuming the included file in the method is something like this, it wasn't in the OP.
<h1><?= $title; ?></h1>
You don't technically need to return anything because the HTML will naturally be caught by the output buffer and displayed when the script finishes executing. However, that is not very clean. The correct way is to take control of the output buffer like this:
public function view($path, $data)
{
$dir = $_SERVER['DOCUMENT_ROOT'].SITE_NAME."/";
extract($data); //variables must exist before importing where they are used.
ob_start();
include($dir.$path.".php"); //capture this output
$view = ob_get_clean();
return $view;
}
$out = $helper->view('test',$data); //out now contains the HTML output from the included file.
Then you can echo it out. IMO this is better because you could then insert that HTML output into another data array and pipe it into another template. Which is useful for things like a re-usable header or footer or nav bar etc..
Consider this
$head['page_title'] = "My Test";
$body['head'] = $helper->view('header',$head); //create head and assign to body
$body['name'] = 'John Smith';
echo $helper->view('body',$body); //create body (with head inserted) and echo
And header.php
<title><?= $page_title; ?></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
And body.php
<!DOCTYPE html>
<html>
<head>
<?= $head; ?>
</head>
<body>
<p><?= $name; ?></p>
</body>
</html>
The output would now be something like this
<!DOCTYPE html>
<html>
<head>
<title>My Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<p>John Smith</p>
</body>
</html>
Now both pages are combined into one, that is output. You could take this to whatever level of re-usability you wanted, but it would save you a ton of typing and make maintaining the views way easier.
But, as I said you can just as simply allow it to output naturally
public function view($path, $data)
{
$dir = $_SERVER['DOCUMENT_ROOT'].SITE_NAME."/";
extract($data); //variables must exist before importing where they are used.
include($dir.$path.".php"); //capture this output
}
//outputs when script is done.
$data['title']into just$titleso i canecho $titlei was looking at the syntax ofextract(array,extract_rules,prefix)i think i am missing the two parameters.$dataand in view simple apply theextract($data);