dongliao8069 2019-06-03 21:08 采纳率: 0%
浏览 63
已采纳

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

It's been a while since I've worked with PHP and am having a hard time figuring out how to call $this from within a class instantiated va ReflectionMethod

I'm instantiating MyClass and want to see some variables, and then access those variables from within a sub class.

$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);
    }
}

I realize I'm instantiating the class and setting the wrapper before setting $foo->bar, so I tried creating another method instead of using the constructor for setting the wrapper, but still can not access $this->bar

// 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')

How can I do this?

  • 写回答

1条回答 默认 最新

  • download20151010 2019-06-06 17:40
    关注

    You cannot access an instance variable within a static method. However, you could pass the instance to your method's params or pass the value as a param like this:

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

    And call it like this:

    Wrapper::css_defaults($this);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上