I have an Admin class:
<?php
Class Admin extends Controller{
function __construct() {
parent::__construct();
}
function getPage(){
$num = 5;
$this->view->load('test');
}
}
?>
the extended Controller class:
<?php
class Controller{
function __construct() {
$this->view = new View();
}
}
?>
view class:
<?php
Class View{
function __construct() {
}
public function load($file){
include($_SERVER["DOCUMENT_ROOT"].'/main/views/'.$file.'.php');
}
}
?>
so in the test.php file i try to echo $num; but i get nothing…
if i try
$num = 5;
include($_SERVER["DOCUMENT_ROOT"].'/main/views/test.php');
it echos back 5
whats the problem here?
You can pass associative array to function
loadas optional parameter and then useextractthat array to have variables in scope.Or
As my personal experience shows second method is slightly faster.
In function
getPage()all you need to do is: