如何在通过ReflectionMethod调用的子类中访问$ this?

时间:2019-06-03 21:08:19

标签: php reflection

自从我使用PHP以来已经有一段时间了,并且很难弄清楚如何从实例化的va $this内的类中调用ReflectionMethod

我正在实例化MyClass,希望看到一些变量,然后从子类中访问这些变量。

$foo = new MyClass('bootstrap');
$foo->bar = 'baz',
class MyClass {

    public $bar = false;

    function __construct($wrapper = '')
    {
        # determine our field wrapper and CSS classes

        if (!$wrapper) {
            # no wrapper, default divs and css
            $this->wrapper = '';
        } else {
            # user-defined wrapper
            $this->wrapper = strtolower($wrapper);
            $wrapper_css = $wrapper . '_css';
        }

        if (!$this->wrapper || in_array($this->wrapper, $this->default_wrapper_types)) {
            # use default css
            $this->controls = Wrapper::css_defaults();
        } else {
            # custom wrapper/control types
            try {
                # check the Controls class for the supplied method
                $method = new ReflectionMethod('wrapper::' . $wrapper_css);
                if ($method->isStatic()) {
                    $this->controls = Wrapper::$wrapper_css();
                }
            } catch (ReflectionException $e) {
                #   method does not exist, spit out error and set default controls
                echo '<span style="color:red">' . $e->getMessage() . '</span>';
                $this->controls = Wrapper::css_defaults();
            }
        }
    }
}

class Wrapper extends MyClass {

    public static function css_defaults() {
        // class names go here...
    }

    public static function bootstrap_css($key = '') {
        // Bootstrap css classes go here...
    }

    public function bootstrap($element = '', $data = '') {
        // Bootstrap form group codes go here...

        // $this->bar is not available
        var_dump($this->bar);
    }
}

我意识到我要在设置$foo->bar之前实例化该类并设置包装器,因此我尝试创建另一种方法,而不是使用构造器来设置包装器,但仍然无法访问$this->bar < / p>

// create the wrapper method...
class MyClass {

    public $bar = false;

    function wrapper($wrapper = '')
    {
        # determine our field wrapper and CSS classes

        if (!$wrapper) {
            # no wrapper, default divs and css
            $this->wrapper = '';
        } else {
            # user-defined wrapper
            $this->wrapper = strtolower($wrapper);
            $wrapper_css = $wrapper . '_css';
        }

        if (!$this->wrapper || in_array($this->wrapper, $this->default_wrapper_types)) {
            # use default css
            $this->controls = Wrapper::css_defaults();
        } else {
            # custom wrapper/control types
            try {
                # check the Controls class for the supplied method
                $method = new ReflectionMethod('wrapper::' . $wrapper_css);
                if ($method->isStatic()) {
                    $this->controls = Wrapper::$wrapper_css();
                }
            } catch (ReflectionException $e) {
                #   method does not exist, spit out error and set default controls
                echo '<span style="color:red">' . $e->getMessage() . '</span>';
                $this->controls = Wrapper::css_defaults();
            }
        }
    }
}

// call it...
$foo = new MyClass();
$foo->bar = 'baz',
$foo->wrapper('bootstrap')

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您不能在静态方法中访问实例变量。但是,您可以将实例传递给方法的参数,也可以将值作为如下参数传递:

public static function css_defaults($instance)
{
    echo $instance->bar; // baz
}

并这样称呼它:

Wrapper::css_defaults($this);