[英]how to call the function of the same class inside another function of that class in php
當我在showAll內調用函數connect和closeDb時,出現錯誤。
class DBConnect {
private $con;
private function connect() {
$con = mysql_connect("localhost", "root", "");
mysql_select_db('school');
}
private function closeDb() {
mysql_close($this->con);
}
public function showAll() {
self::connect();
echo "THis is from the showAll method";
self::closeDb();
}
}
Mysql擴展在PHP 5.5.0中已棄用,在PHP 7.0.0中已刪除。 相反,應使用MySQLi或PDO_MySQL擴展。
了解面向對象編程的基礎知識: http : //php.net/manual/zh/language.oop5.php
這條線看起來是錯誤的。 $con = mysql_connect("localhost", "root", "");
如果想做$this->con = mysql_connect("localhost", "root", "");
然后
public function showAll() { $this->connect(); echo "THis is from the showAll method"; $this->closeDb(); }
public function showAll() { $this->connect(); echo "THis is from the showAll method"; $this->closeDb(); }
使用$ this!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.