I have quite a lot of PHP view files, which I used to include in my controllers using simple include statements. They all use methods declared in a view class which they get like $view->method(); However I recently decided that it would be better if the including would also be done by this view class. This however changes the scope of the included file so that $view is no longer defined. Here is a code example:
in someViewFile.php (BOTH siuations)
<html>
<head><title><?php echo $view->getAppTitle(); ?></title>
etc.
OLD SITUATION in controller:
$view = new view;
include('someViewFile.php'); //$view is defined in someViewFile.php
NEW SITUATION in controller:
$view = new view;
$view->show('someViewFile'); //$view is not defined in someViewFile.php
Right now I hacked my way around the problem using this in the view class:
public function show($file){
$view = &$this;
include($file.".php");
}
Is there anyway to declare the scope of the inluded file or is this the best way of solving the problem?
These examples are off coarse simplified.
You can't change include() scope, no, so your method is probably about as good as it gets in the situation you're describing. Though I'd skip the ugly PHP4-compat ampersand, myself.