0

I have try this answer about require multiple php files, Now I try to require multiple files from multiple directory,but something got error..

this is my directory tree:

test
 -config
  --config_global.php
 -source
  --class
   ---class_core.php
   ---class_load.php
   ---class_test.php
  --include
   ---include_meta.php

class_core.php

<?php
require __DIR__.'/../../config/config_global.php';

class_load.php

<?php
class Load {
    public function loadClass() {
        $this->files = func_get_args();
        foreach($this->files as $file) {
            require __DIR__."/$file.php";
        }
    }

    public function loadInclude() {
        $this->files = func_get_args();
        foreach($this->files as $file) {
            require  __DIR__."/include/$file.php";
        }
    }
}

class_test.php

<?php
$sql = 'SELECT title FROM article WHERE id = 1';
$result = $con->query($sql);

include_meta.php

<?php
$sql = 'SELECT title FROM article WHERE id = 1';
$result = $con->query($sql);

config_global.php

<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_NAME', 'messageboard');
define('DB_PORT', '3306');

$con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, DB_PORT);

index.php

<?php
require dirname(__FILE__).'/source/class/class_core.php';
require dirname(__FILE__).'/source/class/class_load.php';
$load = new Load;
$load->loadClass('class_test');
$load->loadInclude('include_meta');

My question is, why it can't use the $con variable in class_test.php and include_meta.php from config_global.php ?

It display the error:

Undefined variable: con in /source/class/class_test.php
Undefined variable: con in /source/include/include_meta.php
4
  • If you require files in a function, the variables within them are scoped to that function. You'll need to re-architect this a little. Commented Apr 7, 2018 at 8:49
  • So that means I need to add $con variable to all function files? @iainn Commented Apr 7, 2018 at 8:51
  • Well you've got options. You're essentially trying to use global variables here, which isn't really the best design decision. A more object-oriented approach would have your config_global file define a class holding the database connection (and possibly other configuration), which you could then call from other parts of the code. Commented Apr 7, 2018 at 8:57
  • @iainn I got the point,I will try to make an single php just for connecct function Commented Apr 7, 2018 at 9:21

1 Answer 1

1

Try accessing to your $con variable from index.php, it will probably work.

You can't access to your variable after ->loadClass() and ->loadInclude() because you enter inside functions.

Maybe you will be able to access to your $con variable in there if you use $GLOBALS['con'].

Sign up to request clarification or add additional context in comments.

2 Comments

Stand on your point to consider it,is global variable a good choice? Or you will recommend me to use other method?
The best in most of the cases would probably be to integrate a more object-oriented approach for a better architecture.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.