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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错