【问题标题】:How do I reference $this inside of a class { function { function }}? [closed]如何在类 { function { function }} 中引用 $this? [关闭]
【发布时间】: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-&gt;img($matched)

错误:不在对象上下文中使用 $this - 参考 $this-&gt;parseCSS()

里面

Assets::img($matched)

错误:不在对象上下文中使用 $this - 参考 $this-&gt; @987654330 内部@

那么,如何在内部函数中使用$this 访问公共函数?

【问题讨论】:

  • 为什么要在方法中包装函数?这并不像你认为的那样。
  • ^ 这也是不好的做法。可维护性消失了..
  • 兄弟,不要在函数中声明函数。
  • Yo dawg,听说你喜欢函数... :)
  • 你这样做有什么合乎逻辑的理由吗?

标签: php function scope


【解决方案1】:

这样会更合适:

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()
}

您的原始方法会导致每次调用 css 时都会定义 parseCSS - 如果您曾两次调用 css,这可能会导致致命错误。在我修改后的示例中,所有范围问题也更加简单明了。在您的原始示例中,parseCSS 是全局范围内的函数,与您的类无关。

编辑:此处记录了有效的回调公式:http://php.net/manual/en/language.types.callable.php

// Type 1: Simple callback
call_user_func('my_callback_function'); 

// Type 2: Static class method call
call_user_func(array('MyClass', 'myCallbackMethod')); 

// Type 3: Object method call
call_user_func(array($obj, 'myCallbackMethod'));

// Type 4: Static class method call (As of PHP 5.2.3)
call_user_func('MyClass::myCallbackMethod');

// Type 5: Relative static class method call (As of PHP 5.3.0)
call_user_func(array('B', 'parent::who')); // A

//Type 6: Closure
$double = function($a) {
    return $a * 2;
};

$new_numbers = array_map($double, $numbers);

从 PHP 5.4 开始,基于closure 的解决方案也是可能的——这实际上与您最初的意图相似。

【讨论】:

  • 我不知道我可以在第二个参数中包含范围。我曾试图将parseCSS 删除到一个私有函数中,但是将'$this-&gt;parseCSS' 作为preg_replace_callback 中的第二个参数传递并不起作用。您提供的代码效果很好。谢谢!
【解决方案2】:

这并不像你认为的那样。 “内部”函数只是 global 范围内的另一个函数:

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

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

$foo = new Foo();
$foo->bar();
baz();

另外请注意,当多次调用bar 方法时会导致致命错误

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

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

$foo = new Foo();
$foo->bar();
$foo->bar();
baz();

致命错误:无法重新声明 baz()(之前在 /code/8k1 中声明

你应该像Frank Farmer answered那样走,尽管我不会使用public的方法。

【讨论】:

  • 感谢您的信息,我显然混淆了我的一些 javascript 和 php 知识......或者缺乏。
猜你喜欢
  • 2016-04-06
  • 1970-01-01
  • 2011-12-02
  • 1970-01-01
  • 1970-01-01
  • 2013-01-30
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多