Php 如何在类{function{function}中引用$this?

Php 如何在类{function{function}中引用$this?,php,function,scope,Php,Function,Scope,我有一个php类资产。在资产中有各种处理资产的公共功能(缓存、缩小、合并…)。其中一个公共函数包含执行preg\u replace\u callback()所需的辅助函数。此内部函数需要访问其他公共函数之一,但调用其他函数时遇到问题 以下是设置: class Assets { public function img($file) { $image['location'] = $this->image_dir.$file; $image['

我有一个php类
资产
。在
资产中
有各种处理资产的公共功能(缓存、缩小、合并…)。其中一个公共函数包含执行
preg\u replace\u 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
        );

        . . .

    }

}
类别资产
{
公共功能img($file)
{
$image['location']=$this->image\u dir.$file;
$image['content']=文件获取内容($image['location']);
$image['hash']=md5($image['content']);
$image['fileInfo']=pathinfo($image['location']);
返回$this->cache('img',$image);
}
公共功能css($content)
{
. . .
函数parseCSS($matched){
返回$this->img($matched);//此行需要引用函数img()
}
$mend=preg\u replace\u回调(
“#\(.+?)\#i”,
“parseCSS”,
$this->combined_css
);
. . .
}
}
以下是我尝试过的:

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()的内部

资产::img($matched)

错误:不在对象上下文中使用$this-指
$this->
img()的内部


因此,如何从内部函数中使用
$this
访问公共函数?

这样更合适:

public function css($content)
{
    //. . .
    $mend = preg_replace_callback(
        '#\<parse\>(.+?)\<\/parse\>#i',
        array($this, 'parseCSS'),
        $this->combined_css
    );
    //. . .
}

public function parseCSS($matched){
    return $this->img($matched); // THIS LINE NEEDS TO REFERENCE function img()
}

从PHP5.4开始,基于的解决方案也是可能的——这实际上与您最初打算的类似。

这更合适:

public function css($content)
{
    //. . .
    $mend = preg_replace_callback(
        '#\<parse\>(.+?)\<\/parse\>#i',
        array($this, 'parseCSS'),
        $this->combined_css
    );
    //. . .
}

public function parseCSS($matched){
    return $this->img($matched); // THIS LINE NEEDS TO REFERENCE function img()
}

从PHP5.4开始,基于PHP的解决方案也是可能的——这实际上与您最初打算的类似。

这并没有达到您认为的效果。“内部”功能只是全局范围内的另一个功能:

<?php
class Foo
{
    public function bar()
    {
        echo 'in bar';

        function baz() {
            echo 'in baz';
        }
    }
}

$foo = new Foo();
$foo->bar();
baz();
致命错误:无法重新声明baz()(以前在/code/8k1中声明


你应该这样做,尽管我不会公开该方法,但它不会做你认为它做的事情。该“内部”函数只是全局范围内的另一个函数:

<?php
class Foo
{
    public function bar()
    {
        echo 'in bar';

        function baz() {
            echo 'in baz';
        }
    }
}

$foo = new Foo();
$foo->bar();
baz();
致命错误:无法重新声明baz()(以前在/code/8k1中声明


你应该这样做,虽然我不会公开这个方法,但是你为什么要用方法包装函数呢?这并不是你所认为的那样。^这也是一种不好的做法。可维护性消失了..兄弟,不要在函数中声明函数。哟,我听说你喜欢函数…:)你这样做有什么逻辑上的原因吗?为什么要用方法包装函数?这和你认为的不一样。^这也是一种不好的做法。可维护性消失了..兄弟,不要在函数中声明函数。哟,老兄,我听说你喜欢函数…:)您这样做有什么逻辑原因吗?我不知道我可以在第二个参数中包含范围。我曾试图将
parseCSS
删除到一个私有函数中,但将
'$this->parseCSS'
作为
preg\u replace\u回调
中的第二个参数传递失败。您提供的代码工作得非常好。谢谢我不知道我可以在第二个参数中包含scope。我曾试图将
parseCSS
删除到一个私有函数中,但将
'$this->parseCSS'
作为
preg\u replace\u回调
中的第二个参数传递失败。您提供的代码工作得非常好。谢谢谢谢你提供的信息,我显然把我的一些javascript和php知识混在了一起。。。感谢你提供的信息,我显然把我的一些javascript和php知识混在了一起。。。或者说没有。