昨天我完成了使用__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的if-branch内部Body_Style
和Set_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 :(得分: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
)
希望这有帮助!