download92000 2014-05-04 06:48
浏览 46
已采纳

扩展vs不在PHP中扩展类

I'm new. Below is the beginning of an attempt to interface with a database. Please let me know if the syntax is not correct, it seems to work on my localhost.

I think I could have typed class Database extends Mysqli, right? which would then have made the methods of Mysqli directly accessible to Database rather than through an instance created in the class itself. Would that have been preferable to what I have done?

class Database {

    #The variable that stores the database handle
    public $db;

    #The Database objects's datbase parameters
    public $host;
    public $user;
    public $password;
    public $database;

    #creates a Database object with the required databases details
    public function __construct($host, $user, $password, $database) {
        $this->host = $host;
        $this->user = $user;
        $this->password = $password;
        $this->database = $database;
    }

    #Stores the database handle as a var $db of the Database instance 
    public function connect() {
        if ($this->db = new Mysqli($this->host, $this->user, $this->password, $this->database)) {
            if ($this->db->connect_errno) {
                echo "No connection could be made <br/>";
            } else {
                echo "database succesfully connected <br/>";
            }
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dourao1896 2014-05-04 06:56
    关注

    If your class Database represents the database handle, then it should not have it public:

    #The variable that stores the database handle
    public $db;
    

    Otherwise you would not encapsulate that detail and therefore you wouldn't need your class at all.

    Next to that, when you start to write classes, echo does not belong in there:

        if ($this->db = new Mysqli($this->host, $this->user, $this->password, $this->database)) {
            if ($this->db->connect_errno) {
                echo "No connection could be made <br/>";
            } else {
                echo "database succesfully connected <br/>";
            }
        }
    

    Because classes consists of methods returning via their return value and not via standard output. Instead you want to throw an exception here. Which is also a feature of Mysqli already, therefore, you don't need to write that error handling code your own to get started:

    After getting these more or less obvious ones out of the way, you're asking yourself whether or not you should inherit mysqli instead of aggregating it.

    I actually can not tell you. So far the code you've shared just shows standard functionality of a mysqli therefore I would suggest to drop that class completely as the code looks superfluous. So I would say: neither. I see no reason for your Database class as you just could use mysqli instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)