lundi 3 juin 2019

How can I access $this from within a sub class called via ReflectionMethod?

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?





Aucun commentaire:

Enregistrer un commentaire