【发布时间】:2012-10-24 18:09:16
【问题描述】:
我有一个 php 类 Assets。在Assets 中有各种处理资产的公共函数(缓存、缩小、组合......)。其中一个公共功能包含执行preg_replace_callback() 所需的辅助功能。此内部函数需要访问其他公共函数之一,但我无法调用其他函数。
设置如下:
class Assets
{
public function img($file)
{
$image['location'] = $this->image_dir.$file;
$image['content'] = file_get_contents($image['location']);
$image['hash'] = md5($image['content']);
$image['fileInfo'] = pathinfo($image['location']);
return $this->cache('img',$image);
}
public function css($content)
{
. . .
function parseCSS($matched){
return $this->img($matched); // THIS LINE NEEDS TO REFERENCE function img()
}
$mend = preg_replace_callback(
'#\<parse\>(.+?)\<\/parse\>#i',
'parseCSS',
$this->combined_css
);
. . .
}
}
这是我尝试过的:
$this->img($matched)错误:不在对象上下文中使用 $this - 参考
里面$this->在parseCSS()
Assets::img($matched)错误:不在对象上下文中使用 $this - 参考
$this->@987654330 内部@
那么,如何在内部函数中使用$this 访问公共函数?
【问题讨论】:
-
为什么要在方法中包装函数?这并不像你认为的那样。
-
^ 这也是不好的做法。可维护性消失了..
-
兄弟,不要在函数中声明函数。
-
Yo dawg,听说你喜欢函数... :)
-
你这样做有什么合乎逻辑的理由吗?