2013-11-10 149 views
0

我收到以下錯誤定義的方法:PHP不承認

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

有誰知道這是怎麼回事?方法在類內定義。 這部分似乎是這個問題:$this->link = $conn->connect();

類如下:

<?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(); 
     } 
    } 
?> 

方法稱爲通過以下:

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

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

    print_r($data); 
?> 

回答

0

你已經關閉了你的類,所以: function connect(){ /* */ }

超出客體範圍的。

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 

話雖這麼說,database->connect();不會是一個定義的方法..相反:

$Var = connect(); 

將與當前的設置

1
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; 
} // <-- end of class database 

它沒有任何確實的方法。我相信你應該移動這個},如果你想讓函數connect()成爲它的一個方法,並且只在函數之後放置它。

+0

你是正確的工作! – newToStackOverflow