为什么在构造函数中echo和method调用不起作用?

时间:2018-07-18 05:49:13

标签: php

<?php

echo "entering php file";

    class Databasemanager{

        private $server = "localhost";
        private $dbName = "mytest";
        private $user = "root";
        private $pass = ""; 
        private $conn = "";

        function __CONSTRUCT(){
            establishConnection();
            echo "in class constructor";
        }

        private function establishConnection(){
            try {
                $this->conn = mysqli_connect($this->server, $this->user, $this->pass, $this->dbName);
                if ($this->conn) {
                    return true;
                    echo "connection established";
                } else {
                    return false;
                    echo "connection not established";
                }
            }
            catch (Exception $e){}
        }

        function executeQuery($query, $type){

            if($this->establishConnection()){

                    switch($type){
                        case 'read':
                            $data = mysqli_query($this->conn, $query) or die(mysqli_error($this->conn));
                            $result = mysqli_fetch_assoc($data);
                            return $result;
                        break;

                        case 'read_many':
                            $data = mysqli_query($this->conn, $query) or die(mysqli_error($this->conn));
                            return $data;
                        break;


                        case 'update':
                            $a = mysqli_query($this->conn, $query) or die(mysqli_error($this->conn));
                            if($a){
                                return true;
                            }else{
                                return false;
                            }
                        break;

                        case 'insert':
                            $a = mysqli_query($this->conn, $query) or die(mysqli_error($this->conn));
                            if($a){
                                return true;
                            }else{
                                return false;
                            }
                        break;


                    }
            }   

        }


         function closeConnection(){
            mysqli_close($this->conn);
        }




    }

    ?>

1 个答案:

答案 0 :(得分:0)

构造函数中出现错误,导致程序无法继续执行,对establishConnection()的调用应为...

function __construct(){
    $this->establishConnection();
    echo "in class constructor";
}

因此,当您创建一个新对象时...

$test = new Databasemanager();

将显示您的消息。

如果您添加...

error_reporting(E_ALL);
ini_set('display_errors', 1);

在开发文件的开头,这些错误会更清楚地显示出来。