How can I customize variables that all files can access in PHP?

I have a requirement:

in my php web project, I think there is a write variable that can be accessed in all php files. For example:

hold the variable of token. I define it in a tokens.php, but I don"t want to require the tokens.php file anywhere else. How can I access it? And it can be modified, is that okay?

Php
Mar.28,2021

write a function to manage the acquisition, and call this function when you need to get it.

function getToken()
{
    $token = require('tokens.php');
    return $token;
}

files introduced according to the current trend will be automatically introduced by psr4.
you can find information about the registry mode.
ps: wants to be able to use variables directly everywhere. No, it can only be obtained through functions. Is it better to use less global variables


items without composer management? You can use the autoload mechanism. It must be annoying to have require in every file header.


Import a global configuration file

so the variables in the configuration are in the lifecycle of PHP


define an autoload class by yourself, and then use the _ _ call () method to get it automatically, or you can do it directly with session, which is also possible. If it is native, you have to write the autoload class yourself. If it is a framework, there are many global ones, or you can write it into public methods such as tp5's common.php file


entry file mode. All programs will enter the entry file, so you can define it. This is how frameworks are defined.


exists in session or redis

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b30398-4d397.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b30398-4d397.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?