我试图在PHP的另一个scipt中使用公共变量,但我得到两个错误

时间:2016-03-08 06:53:10

标签: php

Config.php

 <?php 
    //Database Connection constants         
    define('DB_HOST' , 'localhost');
    define('DB_USER' , 'root');
    define('DB_PASS' , '');
    define('DB_NAME' , 'gallery_db');
?>

database.php中

<?php 
    require_once ("config.php");

     class Database {

        public $connection;

        function __construct() {

        $this->open_db_connection();
        } 
        public function open_db_connection () {

        $this->connection = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if(mysqli_connect_errno()){
            die("database connection failed badly" . mysqli_error());
        }        
        }        
    }

    // $database = new Database();
    $database = new Database();    

?>

的init.php

 <?php       
     include "config.php";
     include "database.php";        
 ?>

admin_content.php

     <div id="page-wrapper">

                <div class="container-fluid">

                    <!-- Page Heading -->
                    <div class="row">
                        <div class="col-lg-12">
                            <h1 class="page-header">
                                Blank Page
                                <small>Subheading</small>
                            </h1>
                        <?php 


                            if($database->connection) // the line that the error is pointing to
                            {
                                echo "true";
                            }
                        ?>

                            <ol class="breadcrumb">
                                <li>
                                    <i class="fa fa-dashboard"></i>  <a href="index.html">Dashboard</a>
                                </li>
                                <li class="active">
                                    <i class="fa fa-file"></i> Blank Page
                                </li>
                            </ol>
                        </div>
                    </div>
                    <!-- /.row -->

                </div>
                <!-- /.container-fluid -->

            </div>

代码在同一个文件上工作正常,但是当我尝试从不同的文件/ scipt访问公共变量时,会出现以下错误(2)

错误

  

注意:未定义的变量:数据库中   第15行的C:\ xampp \ htdocs \ gallery \ admin \ includes \ admin_content.php

     

注意:尝试获取非对象的属性   第15行的C:\ xampp \ htdocs \ gallery \ admin \ includes \ admin_content.php

2 个答案:

答案 0 :(得分:1)

您忘记在init.php

中加入admin_content.php个文件

admin_content.php文件

的顶部写下面一行
<?php include 'init.php'; ?>

注意: - 您不需要在config.php文件中包含init.php文件,因为该文件已加载到database.php文件中。

答案 1 :(得分:1)

<强>的init.php

<?php       
     include "config.php";
     include "database.php";        
 ?>

编辑(init.php)

<?php       
 include "config.php";
 include "Database.php";        
?>

<强> admin_content.php

//include init.php file
<?php include 'init.php' ?>
<div id="page-wrapper">

                <div class="container-fluid">

                    <!-- Page Heading -->
                    <div class="row">
                        <div class="col-lg-12">
                            <h1 class="page-header">
                                Blank Page
                                <small>Subheading</small>
                            </h1>
                        <?php 


                            if($database->connection) // the line that the error is pointing to
                            {
                                echo "true";
                            }
                        ?>

                            <ol class="breadcrumb">
                                <li>
                                    <i class="fa fa-dashboard"></i>  <a href="index.html">Dashboard</a>
                                </li>
                                <li class="active">
                                    <i class="fa fa-file"></i> Blank Page
                                </li>
                            </ol>
                        </div>
                    </div>
                    <!-- /.row -->

                </div>
                <!-- /.container-fluid -->

            </div>

<强>结果 enter image description here