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
Şu anda ben görünümü sınıfında bu kullanarak soruna yolumu kesmek:
public function show($file){
$view = &$this;
include($file.".php");
}
Inluded dosyanın kapsamını beyan için yine de var mı yoksa bu sorunu çözmenin en iyi yolu nedir?
Bu örnekler kapalı kaba basitleştirilmiş.