我有一個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)
Error: Using $this when not in object context - Refers to
$this->
inside ofparseCSS()
Assets::img($matched)
Error: Using $this when not in object context - Refers to
$this->
inside ofimg()
那麼,我怎麼能訪問公共函數$this
從內部函數內?
你爲什麼要在方法中包裝函數?這不符合你的想法。 – PeeHaa
^這也是不好的做法。可維護性退出窗口。 – Steve
兄弟,不要在函數內聲明函數。 – wesside