Welcome Guest, Not a member yet? Register   Sign In
When autoloading files, variables are not visible in the general scope
#1
Question 

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!
Reply
#2

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%
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

(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?
Reply
#4

Try write code in app/Common.php.
 He load before controller
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#5

(This post was last modified: 01-03-2024, 11:17 AM by bully.)

(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
Reply
#6

https://github.com/codeigniter4/CodeIgni...r.php#L309
Include files ONCE, may be here problem.
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#7

(01-03-2024, 11:38 AM)ozornick Wrote: https://github.com/codeigniter4/CodeIgni...r.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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB