繁体   English   中英

如何在PHP中该类的另一个函数内调用同一类的函数

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM