dpcwz210393 2013-11-10 01:28
浏览 21
已采纳

PHP无法识别类中定义的方法

I get the following error:

Fatal error: Call to undefined method database::connect() in
/Applications/XAMPP/xamppfiles/htdocs/proyectoFinal/core/class.ManageDatabase.php 
on line 8

Does anyone know what is going on? The method IS defined inside the class. This part seems to be the problem : $this->link = $conn->connect();

Class is as follows:

<?php 

include_once('../config.php');

    class database{
        protected $db_conn;
        public $db_name = DB_NAME;
        public $db_host = DB_HOST;
        public $db_pass = DB_PASS;
        public $db_user = DB_USER;  
    }

    function connect(){
        try{
            $this->$db_conn = new PDO("mysql:host = 
                    $this->db_host;dbname=$this->db_name",
                    $this->db_user, $this->db_pass);
            return $this->db_conn;  
        }
        catch(PDOException $e)
        {
        return $e->getMessage();
        }
    }
?>

Methods called by the following:

<?php
    include_once('../core/class.ManageDatabase.php');
    $init = new ManageDatabase;

    $table_name = 'persona';
    $data = $init->getData($table_name);

    print_r($data);
?>
  • 写回答

2条回答 默认 最新

  • duanfa2014 2013-11-10 01:32
    关注

    You have closed off your class, so: function connect(){ /* */ }

    is out of Object Scope.

    class database{
            protected $db_conn;
            public $db_name = DB_NAME;
            public $db_host = DB_HOST;
            public $db_pass = DB_PASS;
            public $db_user = DB_USER;  
        } // Remove this and add it at the end of your class definition 
    

    That being said, database->connect(); will not be a defined method.. Rather:

    $Var = connect();
    

    which will work with the current setup

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序