我目前正在将代码从页面解析器函数重构为 OOP。 我在将文件中的代码包含并运行到主函数范围中时遇到困难:
对象:
class phpFragment {
private $sData;
function render() {
return include $oElement->sData;
}
}
对象容器类:
class pageData {
protected $aPhpFragments;
protected $aCssFragments;
public function outputData($sTag) {
switch($sTag) {
case 'php':
foreach($this->aPhpFragments as $oPhpFragment) {
return $oPhpFragment->render();
}
break;
case 'css':
foreach($this->aCssFragments as $oCssFragment) {
echo $oCssFragment->render();
}
break;
}
}
}
主要功能:
function parsePage($sLanguageCode) {
$oTranslator = new translator($sLanguageCode);
$aTranslations = $oTranslator->translations('page');
$oBuilderClass = new builder($aTranslations);
//... queries to get data and set pagedata and get the template file
$oPageData = $oPage->getData();
$aTemplateTags = $oTemplate->getTags();
foreach($aTemplateTags as $sTag) {
$oPageData->outputData($sTag);
}
//....
}
包含代码(示例):
<?php
$oBuilderClass->build_element(.... parameters here);
?>
我只想启动构建器类一次,因为它包含相当多的数据,并且我不想在每次包含时重新创建它。
如何将包含的代码返回到可以使用 builderClass 的 parsePage 函数中?
最佳答案
您可以创建一个 Context
类,它将作为范围变量的容器,并帮助您在上下文中包含(执行)代码。它将是一个单例类(仅创建一个实例)。
使用方法如下:current()
方法返回当前实例,然后您可以使用export()
方法将变量导出到上下文,它接受一个键/值数组。方法 execute()
将文件名作为参数,并将其与可用的导出变量一起包含在内,您可以添加临时变量作为第二个参数:
//Somewhere before execute();
oContext::current()->export([
'variable1' => 'value1',
'instance' => $instance
]);
//Then anywhere in your file:
oContext::current()->execute("toBeIncluded.php", [
'tmp_variable' => 'tmp_value'
]);
//toBeIncluded.php
echo $variable1;
echo $instance->method1();
echo $tmp_variable;
就您而言:
主要功能:
function parsePage($sLanguageCode) {
$oTranslator = new translator($sLanguageCode);
$aTranslations = $oTranslator->translations('page');
$oBuilderClass = new builder($aTranslations);
//export variables to your context
//Don't be aware of memroy usage objects are passed by reference
oContext::current()->export(compact('oBuilderClass'));
//... queries to get data and set pagedata and get the template file
$oPageData = $oPage->getData();
$aTemplateTags = $oTemplate->getTags();
foreach($aTemplateTags as $sTag) {
$oPageData->outputData($sTag);
}
//....
}
对象:
class phpFragment {
private $sData;
function render() {
oContext::current()->execute($oElement->sData);
}
}
您可以在下面找到类声明:
oContext.class.php
/**
* Class oContext
*/
class oContext {
/**
* The singleton instance
* @var oContext
*/
private static $instance = null;
/**
* the exported variables
* @var array
*/
private $variables = [];
/**
* Return the singleton or create one if does not exist
*
* @return oContext
*/
public static function current() {
if (!self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Export an array of key/value variables
*
* @param $variables
* @return $this
*/
public function export($variables) {
foreach ($variables as $key => $value) {
$this->variables[$key] = $value;
}
return $this;
}
/**
* Include and execute a file in this context
*
* @param $file
* @param array $variables temporary exports will not be added to the context (not available in the next call)
* @return $this
*/
public function execute($file, $variables = []) {
//Populate variables
foreach (array_merge($this->variables, $variables) as $key => $value) {
${$key} = $value;
}
include $file;
return $this;
}
}
我希望这可以帮助您实现目标。
祝你好运。
关于php - 如何将对象包含的文件路径中的代码运行到对象外部的函数范围内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38041298/