【发布时间】:2014-02-15 14:02:27
【问题描述】:
我的 trait ExportOptions 有两个静态变量和两个函数。
变量是$Code 和$ExportType。 $ExportType 的内容在函数Set_ExportType 中设置。并且两个变量在函数Convert_ExportCode中相遇。
函数Convert_ExportCode有以下简单代码:
private function Convert_ExportCode()
{
switch(static::$ExportType)
{
/*
* writes code
*/
case UniT::UNIT_OPTION_END:
self::$GlobalCode[] = $this -> LocalCode;
$Text = preg_replace('/([\n]+)/', "\n", implode('', self::$GlobalCode));
self::$GlobalCode = array();
echo $Text;
break;
/*
* exports code without writing
*/
case UniT::UNIT_OPTION_STEP:
self::$GlobalCode[] = $this -> LocalCode;
$Text = preg_replace('/([\n]+)/', "\n", implode('', self::$GlobalCode));
self::$GlobalCode = array();
return $Text;
break;
/*
* saves part of code
*/
default:
self::$GlobalCode[] = $this -> LocalCode;
for($Order = 0; $Order < count(self::$GlobalCode); $Order++)
{
self::$GlobalCode[$Order] = preg_replace('/([\n]+)/', "\n", self::$GlobalCode[$Order]);
}
}
}
它(当然,作为整个特征)被三个类(CodeGenerator、SimpleAssembler、MenuAssembler_SelectOptgroup)使用。如果这些类不能互相使用,那也没有问题。
CodeGenerator 类被其他类使用,SimpleAssembler 类被MenuAssembler_SelectOptgroup 使用。
我知道(因为我测试了它 - 并且该类位于第一级)CodeGenerator 接受并输出(并处理)变量 self::$Code 的内容,即使它作为文本包装到任何生成的封闭元素代码。
public function Execute()
{
$CodePartNumber = count(self::$Code);
/* some code that not handles with sel::$Code */
// base of code storage into self::$Code
self::$Code[$CodePartNumber] = $this -> Get_AssembledCode(/* arguments for vsprintf */);
return $this -> Convert_ExportCode();
}
SimpleAssembler 类以类似的方式组装和存储代码:
public function Execute()
{
$this -> Check_Orders();
$CodePartNumber = count(self::$Code);
/*
* generation of sub-level
*/
for($Order = 0; $Order < count($this -> Content); $Order++)
{
$VMaX = new CodeGenerator($this -> Elements['sub']['main']);
$VMaX -> Set_Text((empty($this -> Content[$Order]) ? '' : $this -> Content[$Order] ));
if($Order < count($this -> Content)-1 )
{
$VMaX -> Set_ExportType(UniT::UNIT_OPTION_GOON);
$VMaX -> Execute();
}
else
{
$VMaX -> Set_ExportType(UniT::UNIT_OPTION_STEP);
self::$Code[$CodePartNumber] = $VMaX -> Execute();
}
}
if($this -> Disable_TopLevel == FALSE)
{
/*
* generation of top level element and inserting of columns into it
*/
$VMaX = new CodeGenerator($this -> Elements['top']['main']);
$VMaX -> Set_ExportType(static::$ExportType);
$VMaX -> Set_Text(self::$Code[$CodePartNumber]);
return $VMaX -> Execute();
}
else
{
return $this -> Convert_ExportCode();
}
}
但是“SimpleAssembler”类在接受此变量的内容时存在问题。最终输出是
<ol >
<ol >
<li >1</li>
<li >2</li>
<li >3</li>
</ol>
<li >4</li>
<li >5</li>
<li >6</li>
</ol>
改为
<ol >
<li >1</li>
<li >2</li>
<li >3</li>
</ol>
<ol >
<li >4</li>
<li >5</li>
<li >6</li>
</ol>
我想知道怎么解决。
编辑: 我还尝试使用非静态变量来存储各种操作之间的类内代码 - 并且只为该静态变量提供最终代码,即使我添加了
$this -> LocalCode = NULL;
构造函数和析构函数,结果还是一样的。
编辑: 我将问题开头的代码改进为新版本,就像目前一样。
【问题讨论】: