PHP using extract() to pass array values as variables and display it on a page

Viewed 1758

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!

1 Answers
Related