Reputation: 922
I am attempting to create a view($path,$data)
function in php
the main functionality is include a specific file from the directory and pass in data/variable into that page, i managed to create $path
and was able to include the define path, now my next step is pass in the $data
values into my included page, and want to convert each array label as a variable.
My php class is below classes.php
.
define("SITE_NAME", "process");
class helpers {
public function view($path, $data)
{
$dir = $_SERVER['DOCUMENT_ROOT'].SITE_NAME."/";
include($dir.$path.".php");
return extract($data);
}
}
On my index.php
require_once('classes.php');
$helper = new Helpers();
$data['title'] = "My Test";
$data['test'] = "test1";
$helper->view('test',$data);
So now on my test.php i am tring to echo $title
which i assuming it will return the value of My Test
, to test if i am getting the values from index.php
i was able to output the $data
using print_r
Array ( [title] => My Test [test] => test1 )
Any advice how can i achieve? i was trying the extract()
function but don't know if my syntax is correct.Thanks in advance!
Upvotes: 3
Views: 2491
Reputation: 21671
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.
Upvotes: 4