How to access variables defined in method with require_once

时间:2016-02-12 19:46:31

标签: php wordpress class oop

I've been working on a simple class that kindo build on the MVC framework. I'm trying to learn a lot about programming. I'm having a problem with scope. It has quite a simple PHP script and need access a variable birth.

class NUMK {

    public $plugin_name;
    public $version;

    public function __construct( $plugin_name, $version) {

        $this->plugin_name = $plugin_name;
        $this->version = $version;
        $this->loadClient();

    }

    public function pager(){
        if(isset($_POST['birth'])){
            $birth = $_POST['birth'];
            require_once '/html/view/index.php';
        };

    }

}

How can I access $birth in /html/view/index.php. I tried global $birth. No luck.

Thanks

1 个答案:

答案 0 :(得分:0)

You could use the extract function. It receives an array and import variables into the current symbol table, after that you can reference it in the required file, as $birth.

Example:

public function pager(){
    if(isset($_POST['birth'])){
        $data          = array();
        $data['birth'] = $_POST['birth'];
        extract($data, EXTR_REFS);
        require_once '/html/view/index.php';
    };

}