CodeIgniter Forums
When autoloading files, variables are not visible in the general scope - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: When autoloading files, variables are not visible in the general scope (/showthread.php?tid=89079)



When autoloading files, variables are not visible in the general scope - bully - 01-03-2024

I am developing specific software and I have a problem
I am loading a file via autoload. In Autoload.php:
PHP Code:
public $files = [
    APPPATH.'/x/init.php'
]; 
Next, in the controller, I run the code by connecting a specific project file:
PHP Code:
class Home extends BaseController
{
    public function index()
    {
        require APPPATH.'/x/main.php';
    }

In the init.php file I define the variable $x = 100
In the main.php file I try to output it via var_dump($x);
As a result I see NULL! Huh
This problem has been bothering me for a long time
And also this code:
PHP Code:
class Home extends BaseController
{
    public function index()
    {
        global $x;
        var_dump($x);
    }

also returns NULL
Help!


RE: When autoloading files, variables are not visible in the general scope - ozornick - 01-03-2024

Yes, there is such a problem. I can only get a variable from a file main.php But the init.php file is loaded 100%


RE: When autoloading files, variables are not visible in the general scope - bully - 01-03-2024

(01-03-2024, 10:25 AM)ozornick Wrote: Yes, there is such a problem. I can only get a variable from a file main.php But the init.php file is loaded 100%

Something with global scopes, in functions inside main.php, global also does not work and shows NULL. What to use instead of global variables?


RE: When autoloading files, variables are not visible in the general scope - ozornick - 01-03-2024

Try write code in app/Common.php.
 He load before controller


RE: When autoloading files, variables are not visible in the general scope - bully - 01-03-2024

(01-03-2024, 11:01 AM)ozornick Wrote: Try write code in app/Common.php.
 He load before controller

This doesn't work for the libraries I'm connect because vendor/autoload.php is loaded after Common.php
I have many variables that create an instance of a class and they are all global


RE: When autoloading files, variables are not visible in the general scope - ozornick - 01-03-2024

https://github.com/codeigniter4/CodeIgniter4/blob/69fc51f0bbf0a16d0caa4d19162985414d5670d0/system/Autoloader/Autoloader.php#L309
Include files ONCE, may be here problem.


RE: When autoloading files, variables are not visible in the general scope - bully - 01-03-2024

(01-03-2024, 11:38 AM)ozornick Wrote: https://github.com/codeigniter4/CodeIgniter4/blob/69fc51f0bbf0a16d0caa4d19162985414d5670d0/system/Autoloader/Autoloader.php#L309
Include files ONCE, may be here problem.

No, nothing changes
I will have to abandon the framework; a functional programming project is not suitable for it.
I still only need a database library, I don’t make a web site
I have already created a topic and will go look for the library