2015-01-13 26 views
0

昨天我完成了课程的建设,其中我使用了__call方法(前几天介绍过)。但它运行正确,直到我使用__call方法。如何解决它? - 变量包含的内容应该是

其所有代码是

public function __call($Function, array $Parameters) 
{ 
    if(method_exists($this, $Function)) 
    { 
     call_user_func_array(array($this, $Name), $Parameters); 
    } 
    else 
    { 
     try 
     { 
      if(!preg_match('/[A-Za-z]_Style|Attribute/i', $Function)) 
      { 
       throw new MarC_Exception(...); 
      } 
     } 
     catch(MarC_Exception $Exception) 
     { 
      $Exception -> ExceptionWarning(...); 
     } 

     $Function = explode('_', $Function); 
     $Function[0] = strtolower($Function[0]); 

     ... 

     $Options = array('Style', 'Attribute'); 

     if($Function[1] == $Options[0]) 
     { 
      if(strtolower($Function[0]) == $this -> Elements['top']) 
      { 
       array_unshift($Parameters, $Function[0]); 
       call_user_func_array(array($this, 'Set_AllElementStyles'), $Parameters); 
      } 
      else 
      { 
       if($this -> Check_StyleName($Parameters[0])) 
       { 
        array_unshift($Parameters, $Function[0]); 
        call_user_func_array(array($this, 'Set_AllElementStyles'), $Parameters); 
       } 
      } 
     } 
     else 
     { 
      if(strtolower($Function[0]) == $this -> Elements['top']) 
      { 
       array_unshift($Parameters, $Function[0]); 
       call_user_func_array(array($this, 'Set_AllElementAttributes'), $Parameters); 
      } 
      else 
      { 
       if($this -> Check_AttributeName($Parameters[0])) 
       { 
        array_unshift($Parameters, $Function[0]); 
        call_user_func_array(array($this, 'Set_AllElementAttributes'), $Parameters); 
       } 
      } 
     } 
    } 
} 

,但问题是,(此时)在preg_match使用。在那里他看到(我不知道为什么)变量函数的内容是Set_AllElementStyles(我在call_user_func_array下面调用)代替(例如)Body_Style。

如果地方为代码echo $Function,看看到底发生了什么,它会调用

  • Body_style如果是在功能代码开头或内如果分支的if-else语句
  • Body_StyleSet_AllElementStyles如果在if-else的其他分支中

我在哪里发生了导致此问题的错误?(?如何解决此问题)

编辑1:类RootAssembler_Html的对象的 实施例(即抽象类UniqueAssembler的最终覆盖),具有__call用法在一起。

$VMaX = new MarC\RootAssembler_Html(); 
$VMaX -> Set_ExportWay(); 
$VMaX -> Set_Content(); 
$VMaX -> Set_Content($Text); 
$VMaX -> Body_Style('background-color', '#ABCDEF'); 
$VMaX -> Body_Attribute('id', 'test'); 
$VMaX -> Execute(); 

输出:

<html> 
    <head> 
    /* some text that is not set in the first usage of method Set_Content */ 
    </head> 
    <body id='test' style="background-color: #ABCDEF;"> 
    /* some text that was set in the second usage of method Set_Content */ 
    </body> 
</html> 
+0

我认为你应该提供一些较短的工作代码,这样我们就可以更容易地看到问题所在 - 并且你会告诉我们你尝试了一些调试......顺便说一句。你知道你的正则表达式/ [a-z] \ _ St​​yle | Attribute/i匹配:单词“属性”还是一个字母后跟'_'和“样式”? –

+0

@JanLegner:问题不在于正则表达式本身(即使它可能错误)。问题是'Set_AllElementStyles'不应该出现在那里。 –

+0

@Václav如何调用具有参数的'__call'以及实际输出和期望输出的示例? – divaka

回答

1

我敢肯定你的问题是这一行:

call_user_func_array(array($this, $Name), $Parameters)

尝试将其更改为:

call_user_func_array(array($this, $Function), $Parameters)

并应该工作。

因为我看不到$Name变量在哪里定义。它应该作为参数传递给函数或成为全局函数。但是,在这两种情况下,我都看不出有什么关键。

而且我建议你用下面的一个,并获取你想要设置属性或者样式类型OT html元素改变你的正则表达式:

preg_match('/([A-Za-z]+)_(Style|Attribute)/i', $Function, $matches)

下面的例子输入:

preg_match('/([A-Za-z]+)_(Style|Attribute)/i', "Body_Style", $matches1); 
preg_match('/([A-Za-z]+)_(Style|Attribute)/i', "Div_Style", $matches2); 
preg_match('/([A-Za-z]+)_(Style|Attribute)/i', "Div_Attribute", $matches3); 

输出:

Array 
(
    [0] => Body_Style 
    [1] => Body 
    [2] => Style 
) 
Array 
(
    [0] => Div_Style 
    [1] => Div 
    [2] => Style 
) 
Array 
(
    [0] => Div_Attribute 
    [1] => Div 
    [2] => Attribute 
) 

希望这有助于!

+0

在上面代码中使用'$ Name'而不是'$ Function' - 我知道应该有'$ Function'。在我的代码中,它是正确的。最后,在将call_user_func_array的用法改为经典的这些函数调用(除了Set_AllElementsStyles之外,当然也包括Set_AllElementsAttributes)之后,问题消失了。哎呀,目前我发现了我在那里的下一个错误。 –

相关问题