简体   繁体   English

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

[英]how to call the function of the same class inside another function of that class in php

when i am calling the function connect and closeDb inside showAll there is an error. 当我在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 extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Mysql扩展在PHP 5.5.0中已弃用,在PHP 7.0.0中已删除。 Instead, the MySQLi or PDO_MySQL extension should be used. 相反,应使用MySQLi或PDO_MySQL扩展。

Learn basics of object oriented programming: http://php.net/manual/en/language.oop5.php 了解面向对象编程的基础知识: http : //php.net/manual/zh/language.oop5.php

This Line looks false. 这条线看起来是错误的。 $con = mysql_connect("localhost", "root", "");

If think you want to do $this->con = mysql_connect("localhost", "root", ""); 如果想做$this->con = mysql_connect("localhost", "root", "");

Then 然后

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(); } use $this ! 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