[英]In PHP, how can I reach my class/public function?
I am working with some php code where I try to reach a class/public function so I can execute a if/else function with the database connected. 我正在使用一些php代码,尝试到达类/公共函数,以便可以在连接数据库的情况下执行if / else函数。 I have only worked with another php file before (dirname( FILE ) but I am not sure how I should adjust that code to reach the class/public function immediately now as they are on the same "page"/php file. 我之前只使用了另一个php文件(dirname( FILE ),但是我不确定如何调整该代码以立即到达类/公共函数,因为它们位于同一“页面” / php文件中。
class ConnectionInfo
{
public function GetConnection()
{
$this->conn = mysqli_connect("localhost", "user","pass", "db") or die(mysqli_error($mysql_pekare));
}
}
$connectionInfo = new ConnectionInfo();
$connectionInfo->GetConnection();
if (!$connectionInfo->conn)
{
//Connection failed
}
else
{
//Connection succeded
}
One way would be to make the variable $conn
from ConnectionInfo
public and use it in your object afterwards: 一种方法是将ConnectionInfo
的变量$conn
公开,然后在您的对象中使用它:
class ConnectionInfo {
public var $conn = null;
public function GetConnection() {
$this->conn = mysqli_connect("localhost", "user","pass", "db") or die(mysqli_error($mysql_pekare));
}
}
And later: 然后:
if (!$connectionInfo->conn) {
//Connection failed
}
Another would be to return
the function value in GetConnection()
. 另一个方法是在GetConnection()
return
函数值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.