How to achieve “require_global” in PHP Server Side Scripting Language?

Asked By: Vajrin Rai

Available Answers: 1

Answer #1#
Jabalpur, India

You can add local variables defined within dosomethingFunction() to global scope:

class wrapController extends baseController {"    function dosomethingFunction() {"        require 'old_dosomething.function.php';"        //begin added code  "        $vararr = get_defined_vars();"        foreach($vararr as $varName => $varValue) "              $GLOBALS[$varName] = $varValue;            "        //end added code          "        $this->view('old_dosomething_view');"    }"}

Note, that for this to work as expected, you should call require before using any other thing in the function. get_defined_vars() returns only variables from the current scope, so no array_diff hacks are needed.

Comment Below


Would you like to join Ask Sawal!


  1. Ask Sawal is a fast growing question and answer discussion forum.

  2. 25 lakh+ questions were answered by our Ask Sawal Members.

  3. Each day 1000s of questions asked & 1000s of questions answered.

  4. Ask any question and get answer from 5 Lakh+ Ask Sawal Members.

  5. Constant moderation and reporting option makes questions and answers spam free.

  6. And also, we have free blogging platform. Write an article on any topic.

  7. We have 10000+ visitors each day. So a beneficial platform for link building.

  8. We are allowing link sharing. Create backlinks to your blog site or any site.

  9. Gain extra passive income by sharing your affiliate links in articles and answers.