我来自程序设计的世界,汇编程序是我的第一语言,而PL / 1和Cobol是我从大部分(坏)习惯中学习的语言。
这是我在这里发表的第一篇文章,如果我没有100%正确,请接受我的道歉。
我正在将我们的前端和后端系统从程序性PHP重写为OOP,我真的不明白要使用什么结构。
在旧系统中,我们有许多脚本,其中包含xxx.inc.php和所使用的函数,而xxx.inc.php又包含其他xxx.inc.php,例如db.inc.php,api_pj .inc.php等
尝试这样做OOP,我为每个类创建一个文件并自动加载它们,但我无法理解如何处理公共类(数据库,连接到外部api等)。在测试时,我使用了继承,它工作正常,但感觉很奇怪。我真的看不到客户类是数据库类的孩子。 另外,我不明白需要定义什么,而不是。是否应该定义类中的所有变量?
以下示例不起作用,因为Customer类的数据库连接不可用。当“类客户扩展数据库”而不是一切正常时,我想有更正确的方法吗?
在Alex Andrei的回复
之后编辑了以下代码the_example_page.php
// autoloader
spl_autoload_register(function ($class) {
require_once 'classes/' . $class . '.class.php';
});
// data for testing
$customer_id = '12090';
$order_id = '31480';
// db
$db = new DB();
$db_conn = $db->db_connect();
// get customer name data and print it:
$customer = new Customer($db_conn);
$customer_data = $customer->get_customer_info($customer_id);
print $customer_data['name'];
// get order date and print it:
$order = new Order($db_conn);
$order_data = $order->get_order_info($order_id);
print $order_data['date'];
DB.class.php
class DB
{
public function __construct() {}
public function db_connect()
{
static $db_conn;
if( !$db_conn )
{
$db_conn = pg_connect("dbname=database user=user password=PaSsWoRd");
}
return $db_conn;
}
public function db_exec($sql)
{
if( !$sql ) return;
$db_conn = $this->db_connect();
$result = @pg_exec($db_conn,$sql);
if( !$result )
{
return;
}
$ret[result] = $result;
return $ret;
}
public function db_getrow(&$a)
{
# a bunch of stuff in real function, but here only a plain fetch_array as example
$ret = pg_fetch_array($a);
return $ret;
}
}
Customer.class.php
class Customer
private $conn;
{
public function __construct($db)
{
$this->conn=$db;
}
public function get_customer_info($customer_id)
{
return $this->conn->db_getrow($this->conn->db_exec("SELECT * FROM customer WHERE customerid = $customer_id;"));
}
public function get_all_customers($status)
{
return $this->conn->db_exec("SELECT * FROM customer WHERE status = $status;");
}
}
Order.class.php
class Order
{
private $conn;
{
public function __construct($db)
{
$this->conn=$db;
}
public function get_order_info($order_id)
{
return $this->conn->db_getrow($this->conn->db_exec("SELECT * FROM order WHERE orderid = $order_id;"));
}
public function get_all_orders($status)
{
return $this->conn->db_exec("SELECT * FROM order WHERE status = $status;");
}
}
答案 0 :(得分:1)
UPDATE
关于如何修改DB连接类和Order类的扩展示例
数据库类
class DB
{
private $conn;
private $user;
private $password;
private $database;
public function __construct($database,$username,$password){
$this->user = $user;
$this->password = $password;
$this->database = $database;
}
public function connect(){
if( !$this->conn ){
$this->conn = pg_connect("dbname=$this->database user=$this->user password=$this->password");
}
return $this->conn;
}
public function db_exec($sql){
if( !$sql ) return; // add some relevant error message
if (!$this->conn){
$this->connect();
}
$result = pg_exec($this->conn,$sql);
if( !$result ){
return;
}
$ret[result] = $result;
return $ret;
}
public function db_getrow(&$a){
# a bunch of stuff in real function, but here only a plain fetch_array as example
$ret = pg_fetch_array($a);
return $ret;
}
}
订单类
class Order
{
private $dbObj;
public function __construct($db)
{
$this->dbObj=$db;
}
public function get_order_info($order_id)
{
$result = $this->dbObj->db_exec("SELECT * FROM order WHERE orderid = $order_id;");
return $this->dbObj->db_getrow($result);
}
public function get_all_orders($status)
{
return $this->conn->db_exec("SELECT * FROM order WHERE status = $status;");
}
}
用法
$db = new DB("myDatabase","user","password");
$db->connect();
$order = new Order($db);
你不必extend
所有事情,只有有意义的事情。
只需将db连接作为参数传递给其他对象,即可执行查询。
实施例
$customer = new Customer($db);
$order = new Order($db);
您应该为每个类添加一个私有变量来保存数据库连接,例如
class Customer{
private $conn;
// ...
}
,构造函数看起来像这样
public function __construct($db) {
$this->conn = $db;
}
在类似的方法中使用它
public function get_order_info($order_id){
return $this->conn->db_getrow($this->db_exec("SELECT * FROM order WHERE orderid = $order_id;"));
}
答案 1 :(得分:-1)
如果他们使用function isEmptyObj(obj) {
for(var prop in obj) {
if (obj[prop] != "") return false; //or some other checking
}
return true;
}
类,则可以extend
其他类,然后在子构造函数中调用它们。如果选择这种方法,请注意方法中存在冲突的变量。
如果database
类扩展了Customer
类,那么它将可以访问Database
等方法
Database