我正在努力熟悉OOP。到目前为止,我知道如何制作课程方法,继承,静态等。我正在制作酒店预订系统(非常基本)。
问题在于,根据我对实施OOP方法的理解,但如果我做对了,我仍感到困惑。功能性工作正常,但我对结构不满意。
我创建了一个类数据库,客户,房间 这些是非常基本的类,如数据库有connect和disconnect方法以及静态函数run();运行sqli查询。在客户和房间类中,我有CRUD方法基本上调用带有不同参数的数据库静态run()方法来执行我的crud操作。
我正在做对吗?现在我必须为我的应用程序和房间预订模块创建用户。我应该如何向前推进..我已经失去了我应该在用户类中放置什么以及预订时应该怎样.../基本上我没有太多的想法..在什么时候我们意识到我们必须上课?
如果我创建了一个用户类并使方法login()logout()可以吗?
我想我并没有准确地描述自己,但是考虑到我的初学者,你们就会得到我的困惑点。
非常感谢你。
示例类:
<?php
class Customers {
/*
* Declaring properties
*/
private $customer_id;
private $customer_name;
private $customer_cnic;
private $customer_address;
private $customer_email;
private $customer_phone;
/**
* View customers list
* @return array of rows
*/
public function View_Customers() {
$result = Database::Run("SELECT * FROM customer");
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
public function View_Single_Customer($Id) {
$result = Database::Run("SELECT * FROM customer WHERE id=" . $Id);
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
/**
* Add a new customer
* @param array $arg
* return last insert id on success
*/
public function Add_Customer($arg) {
$this->customer_name = $arg[0];
$this->customer_cnic = $arg[1];
$this->customer_address = $arg[2];
$this->customer_email = $arg[3];
$this->customer_phone = $arg[4];
$result = Database::Run("INSERT INTO customer (name,cnic,address,email,phone)
VALUES ('" . $this->customer_name . "','" . $this->customer_cnic . "','" . $this->customer_address . "',
'" . $this->customer_email . "','" . $this->customer_phone . "')");
return mysqli_insert_id(Database::$connection);
}
/**
* Delete a specific customer
* @param int or array of ids $id
* return affected rows on success
*/
public function Delete_Customers($id) {
$result = Database::Run("DELETE FROM customer WHERE id IN ($id)");
return $id;
}
/**
* Update existing customer based on id
* @param array $arg
* return id of updated record
*/
public function Update_Customer($arg) {
$this->customer_id = $arg[0];
$this->customer_name = $arg[1];
$this->customer_cnic = $arg[2];
$this->customer_address = $arg[3];
$this->customer_email = $arg[4];
$this->customer_phone = $arg[5];
if ($result = Database::Run("UPDATE customer SET name='" . $this->customer_name . "', cnic='" . $this->customer_cnic .
"', address='" . $this->customer_address . "', email='" . $this->customer_email . "', phone='" . $this->customer_phone . "' WHERE id=" . $this->customer_id)) {
return 'Record with id '.$this->customer_id.' has been updated ';
}
}
}
答案 0 :(得分:3)
当您可以为该类型的每个对象指定一组特定的属性和方法时,创建一个类。例如,我在学校的住宿和餐饮服务部门进行网络开发工作,我们正在准备一个即将“上线”的RSVP流程
我们有几个阶段,在每个阶段,允许学生根据特定标准回复(偏好)一个房间。每个阶段都有相同的信息,例如开始日期/时间,结束日期/时间,阶段名称,数据库中阶段的ID,以及一个数组,其中包含具有更具体的开始日期/时间的学生与我们(我们的宿舍或公寓)住在一起,指定数量的学期。
为此,我创建了一个包含每个属性的Phase类 - 而不是修改每个Phase的属性或属性发生更改的位置,它们只需要对Phase类进行更改并添加/删除/修改现有属性属性和功能。这节省了编码时间。
要掌握OOP,请阅读Enapsulation