[英]PHP does not recognize method defined in class
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();
这部分似乎是问题所在: $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);
?>
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
It doesn't have any methods indeed. 它确实没有任何方法。 I believe you should move this }
if you want the function connect()
to become one of its methods, and place it only after the function. 我认为,如果希望函数connect()
成为其方法之一,并且只将其放在函数之后,则应移动此}
。
You have closed off your class, so: function connect(){ /* */ }
您已关闭课程,所以: 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();
话虽这么说, database->connect();
will not be a defined method.. Rather: 不会是已定义的方法。
$Var = connect();
which will work with the current setup 将与当前设置一起使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.