简体   繁体   English

抽象类'n接口,专门用于PHP

[英]Abstract class 'n Interface, specifically in PHP

This question is in continuation to my previous post located here . 这个问题是我以前在这里发布的文章的继续。

Since there is no way to post code sample again without editing your original post, I am starting a new post. 由于无法在不编辑原始帖子的情况下再次发布代码示例,因此,我将开始新的帖子。 And also, this contains PHP code. 而且,它包含PHP代码。

I am writing two classes, first is for opening and closing connection to the database, and the second is to provide various reusable functions for DB access. 我正在编写两个类,第一个是用于打开和关闭与数据库的连接,第二个是为数据库访问提供各种可重用的功能。

Below is my PHP code for the Connection class: 下面是我的Connection类的PHP代码:

<?php
/**
* DBConnection Base Class Definition
*/

/* Include dependency */
require_once("\config\dbconfig.php");

abstract class dbconnection 
{
 var $conn;
 //Opens connection for a MySQL DB
 abstract function OpenConnection()
 {
  $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) 
     or die($this->ShowError(mysql_error()));
  mysql_select_db(DB_NAME) or die ($this->ShowError("Connection to MySQL Database Failed. "));  
 }

 //Closes connection for a MySQL DB
 abstract function CloseConnection()
 {
  try
  {
   mysql_close($conn);
  }
  catch(exception $ex)
  {
   $this->ShowError($ex);
  }
 }

 protected function ShowError($err)
 {
  echo "<script>alert('Error: ' +". $err .");</script>"; 
 }  
}
?>

The file included in the above code contains following code: 上面的代码中包含的文件包含以下代码:

<?php
//Modify constants with data needed to access your own database
define('DB_HOST','localhost:3306');
define('DB_NAME','MyStore');
define('DB_USER','super');
define('DB_PASSWORD','****');
?>

Now here are my queries related to abstract class and interfaces (continued from my previous post): 现在,这是我与抽象类和接口有关的查询(接上我的上一篇文章):

(1) Can a connection class be abstract? (1)连接类可以抽象吗? Are the methods written correctly? 方法编写正确吗?
(2) Instead of making the second code a file "dbconfig.php", will it be good to make an Interface for it? (2)与其将第二个代码用作文件“ dbconfig.php”,不如为其创建一个接口?

I don't see any reason why you should mark this method as abstract. 我看不出您为什么要将此方法标记为抽象的任何原因。 It is possible, but with what purpose? 可能,但是目的是什么? The methods should be defined without a body if declared abstract. 如果声明为抽象,则应在没有主体的情况下定义方法。 If you want to force a way in connecting than you are better of with defining an interface and implementing it. 如果您想强制一种连接方式,那么比定义一个接口并实现它更好。

I't is not smart to define your db configs as constants in this way. 我不是很聪明以这种方式将您的数据库配置定义为常量。 You probably be better of by giving them as a parameter. 通过将它们作为参数,可能会更好。 That way you are more flexible in connecting with multiple db's for example. 这样,您可以更灵活地连接多个数据库。

(1a) yes (1a)是

(1b) it seems so. (1b)似乎是这样。 But you shouldn't use die in OOP please use exceptions . 但是你不应该在OOP中使用die ,请使用异常

(2) In general it might be better OOP-style to use an interface. (2)通常,使用接口可能是更好的OOP风格。 For PHP in this case it is totally ok to use constants. 在这种情况下,对于PHP,使用常量是完全可以的。 But this is strongly subjective. 但这是很主观的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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