Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP面向对象的酒店应用程序_Php_Oop_Object - Fatal编程技术网

PHP面向对象的酒店应用程序

PHP面向对象的酒店应用程序,php,oop,object,Php,Oop,Object,目前我正在编写一个面向对象的酒店应用程序来学习OOP。 我之所以选择这个,是因为在我的书(O'Reilly的PHP设计模式)中,他们为一家汽车租赁公司编程。 现在我已经完成了基本的业务逻辑,但仍然存在一些问题 在Hotel类中,有以下方法: //All public functions, left it hhere cause of the length checkOut( HotelRoom $room, DateTime $from, DateTime $to ) changeRoom( H

目前我正在编写一个面向对象的酒店应用程序来学习OOP。 我之所以选择这个,是因为在我的书(O'Reilly的PHP设计模式)中,他们为一家汽车租赁公司编程。 现在我已经完成了基本的业务逻辑,但仍然存在一些问题

在Hotel类中,有以下方法:

//All public functions, left it hhere cause of the length
checkOut( HotelRoom $room, DateTime $from, DateTime $to )
changeRoom( HotelRoom $oldRoom, HotelRoom $newRoom, HotelCustomer $customer, DateTime $from, DateTime $to)
checkOut( HotelRoom $room, DateTime $from, DateTime $to )
因此,对于我所做的每一步(预订、更改房间或结帐),我都必须将HotelRoom作为参数传递。每个房间都有一个id和一个号码。 实现方法
addRoom(HotelRoom$room)
并将所有房间存储在受保护的属性
$rooms
数组中,然后只传递方法的
HotelRoom::$id
是否更好


我对OOP比较陌生,现在只想知道什么是一个好的实践。

您可以这样做,但是您可以使用函数
loadroom()
,而不是
addRoom()
,它利用数据库访问对象来加载所有文件室。在预订时,如果您只想加载免费房间,则同样适用于更换房间。您不需要在
checkout()
中执行此操作,我将按照以下方式执行:

添加对象预订,其中包含酒店房间和客户的from、to和reference 然后,更衣室成为一种预订方式,它只改变房间,而不改变日期。 结帐也成为一种预订方式,因为提供结帐日期是并没有意义的。 房间可以容纳可用的时间和不可用的时间,并且应该为此提供方法。 酒店拥有所有的房间,人们应该总是从酒店对象那里得到房间

Hotel
getRoom($id)
getAvailableRooms($from, $to)


HotelRoom
checkIn($from, $to) - proxy to reserve($from, $to) - sets the availability
free($from, $to)

Booking
changeRoom($newRoom)
changeDates($from, $to) // this might be tricky, as it may require changing the room as well
checkOut() // sets the room from the current date to the end of the booking (in case of early checkout) as available

从技术上讲,这两种方法是相似的。根据干净的编码,传递房间对象而不是数字更好,因为代码更可读。任何使用您的类的人都会知道他使用的是“房间”,而不仅仅是一个数字。

我不会让您的
Hotel
类负责您提到的三个功能。它们是非常具体的功能,而
酒店
是一个非常广泛的类别

考虑使用
RoomManager
CustomerManager
类。将这些类注入
Hotel
类,并让它们负责检索
房间和
客户的房间。
房间
客户
类应包含您概述的特定功能:

class Hotel
{
    public $roomManager;
    public $customerManager;

    public function __construct(RoomManager $rm, CustomerManager $cm)
    {
        $this->roomManager = $rm;
        $this->customerManager = $cm;
    }

    // ...
}

class RoomManager
{
    public function getRoom($id)
    {
        // find room where Room->id == $id;
        return $room;
    }

    // some other stuff may become relevant in here
}

class CustomerManager
{
    public function getCustomer($id)
    {
        // find customer where Customer->id == $id;
        return $customer;
    }

    // some other stuff may become relevant in here
}

class Room
{
    public function checkout(DateTime $from, DateTime $to)
    {
        // ...
    }
}

class Customer
{
    private $room;

    public function setRoom(Room $hr)
    {
        $this->room = $hr;
    }
}
客户端代码类似于:

// instantiate a Hotel and inject its 2 dependencies
$hotel = new Hotel(new RoomManager, new CustomerManager);

// checkout Room 3
$hotel->roomManager->getRoom(3)->checkout();

// change Customer 2's Room from Room 3 to Room 4
$newRoom = $hotel->roomManager->getRoom(4);
$hotel->customerManager->getCustomer(2)->setRoom($newRoom);

请注意,您的类的责任变得更加具体。
Hotel
类只想管理特定的组件。

为什么要这样做?它能解决什么问题?@SergioTulentsev我不认为有什么问题需要解决,只是增强了代码——或者我读错了吗?最好不要使用受保护的阵列,因为您将根据其ID在相应的酒店房间中搜索每个呼叫。。。在我看来,直接传递对象是正确的方式opinion@hd.:是的,我的意思是,为什么为了改变而改变代码?显然一定会有某种胜利。在这里我看不到任何东西。@SergioTulentsev啊,好的。我现在明白你的逻辑了:)你是说“roxy保留”模式吗?*代理——15个字符