Problems with the global scope of php

in the part where thinkphp5 is used as the site configuration item to take effect in real time

read the data in the configuration table in the app_init hook function, which can be read in all pages. My idea is to use $GLOABLS. But I see other people"s code using the v function to achieve the same effect as $GLOABLS.

$data=Db:table("config")->find();
$GLOBALS["config"]=$data;//
v("config)=$data;//

with v function code

// 
if ( ! function_exists("v")) {
    /**
     * 
     *
     * @param null   $name  
     * @param string $value 
     *
     * @return array|mixed|null|string
     */
    function v($name = null, $value = "[null]")
    {
        static $vars = [];
        if (is_null($name)) {
            return $vars;
        } else if ($value == "[null]") {
            //
            $tmp = $vars;
            foreach (explode(".", $name) as $d) {
                if (isset($tmp[$d])) {
                    $tmp = $tmp[$d];
                } else {
                    return null;
                }
            }

            return $tmp;
        } else {
            //
            $tmp = &$vars;
            foreach (explode(".", $name) as $d) {
                if ( ! isset($tmp[$d])) {
                    $tmp[$d] = [];
                }
                $tmp = &$tmp[$d];
            }

            return $tmp = $value;
        }
    }
}

I don"t quite understand how this function can implement variables that can be accessed by all files in the project. Please advise me one or two
maybe I don"t understand the scope of php enough

Apr.10,2021

he introduces public files in each file and defines v to the current scope. In order to prevent duplication, he checks whether the current scope has been introduced into public files many times before defining, and then continues to define v

if not.
Menu