抽象类和接口,特别是在 PHP 中
这个问题是我上一篇文章的延续,位于此处。
由于无法在不编辑原始帖子的情况下再次发布代码示例,因此我将开始一篇新帖子。而且,它还包含 PHP 代码。
我正在编写两个类,第一个是用于打开和关闭与数据库的连接,第二个是为数据库访问提供各种可重用的函数。
下面是我的 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>";
}
}
?>
上面代码中包含的文件包含以下代码:
<?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','****');
?>
现在这是我与抽象类和接口相关的查询(接上一篇文章):
(1) 连接类可以是抽象的吗?方法写得正确吗?
(2) 与其将第二个代码制作为文件“dbconfig.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.
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:
<?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? 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不明白为什么您应该将此方法标记为抽象方法。这是可能的,但目的是什么?如果声明为抽象方法,则应在没有主体的情况下定义方法。如果您想强制采用一种连接方式,那么最好定义一个接口并实现它。
我以这种方式将数据库配置定义为常量并不明智。您最好将它们作为参数提供。例如,这样您就可以更灵活地连接多个数据库。
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) 是的
(1b) 看起来是这样。但是你不应该在 OOP 中使用
die
请使用 例外。(2) 一般来说,使用接口可能是 OOP 风格更好的选择。对于这种情况下的 PHP 来说,使用常量是完全可以的。但这是非常主观的。
(1a) yes
(1b) it seems so. But you shouldn't use
die
in OOP please use exceptions.(2) In general it might be better OOP-style to use an interface. For PHP in this case it is totally ok to use constants. But this is strongly subjective.