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
carry0987
  • 117
  • 1
  • 2
  • 14
  • 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. – iainn Apr 07 '18 at 08:49
  • So that means I need to add $con variable to all function files? @iainn – carry0987 Apr 07 '18 at 08: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. – iainn Apr 07 '18 at 08:57
  • @iainn I got the point,I will try to make an single php just for connecct function – carry0987 Apr 07 '18 at 09:21

1 Answers1

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'].

Sébastien S.
  • 1,444
  • 8
  • 14
  • Stand on your point to consider it,is global variable a good choice? Or you will recommend me to use other method? – carry0987 Apr 07 '18 at 09:04
  • The best in most of the cases would probably be to integrate a more object-oriented approach for a better architecture. – Sébastien S. Apr 07 '18 at 09:09