4

PHP面向对象设计简单示例

 1 year ago
source link: https://www.iplayio.cn/post/003199467
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

PHP面向对象设计简单示例

发布:5小时前 更新:5小时前 folder_open 计算机编程语言 PHP编程 comment

原文地址:PHP面向对象设计简单示例

一个相对完整的PHP面向对象设计简单示例

<?php

abstract class SystemAdminRole
{
    abstract public function can();
}

abstract class Authority
{
    abstract public function check(SystemAdminRole $systemAdminRole);
}

class AdminAuthority extends Authority
{
    public function check(SystemAdminRole $systemAdminRole)
    {
        return $systemAdminRole->can();
    }

}


class AdminRole extends SystemAdminRole
{
    public function can()
    {
        
    }

}

abstract class SystemUserRole
{

}

abstract class SystemAdmin
{

}

abstract class User
{
    public function getUserInfo()
    {

    }

    public function getUserId()
    {

    }

    public function getUserMobile()
    {

    }

    public function getUserPassword()
    {

    }

    public function changePassword()
    {

    }

    //提现
    abstract function withdraw();
}

abstract class SystemManger extends User
{

}

abstract class SystemUser extends User
{
    abstract public function cancelOrder($orderInfo);
}

class Admin extends SystemAdmin
{

}

class Agent extends SystemManger
{
    function withdraw()
    {
        // TODO: Implement withdraw() method.
    }

}

class Distributor extends SystemManger
{
    function withdraw()
    {
        // TODO: Implement withdraw() method.
    }

}

class Customer extends SystemUser
{
    public function createOrder($data)
    {

    }

    public function pay()
    {

    }

    public function cancelOrder($orderInfo)
    {
        if ($orderInfo['user_id'] !== 1) {
            echo "您无权取消该订单\n";
        }

        //判断订单状态

        //返回结果
        echo "取消订单成功\n";
    }

    function withdraw()
    {
        throw new Exception("您无权提现\n");
    }


}

class Driver extends SystemUser
{
    //接单
    public function takeOrder()
    {

    }

    //接乘客
    public function pickUpCustomer()
    {

    }

    //完成订单
    public function finishOrder()
    {

    }


    //取消订单
    public function cancelOrder($orderInfo)
    {
        if ($orderInfo['driver_user_id'] !== 2) {
            echo "您无权取消该订单\n";
        }

        echo "取消订单成功\n";
    }

    function withdraw()
    {
        echo "提交成功,请等待管理员审核\n";
    }


}

class Order
{

    public function create(SystemUser $systemUser, $data)
    {
        return $systemUser->createOrder($data);
    }

    public function update()
    {

    }

    public function delete()
    {

    }

    public function finish(SystemUser $systemUser, $orderInfo)
    {
        return $systemUser->finishOrder();
    }

    public function cancel(SystemUser $systemUser, $orderInfo)
    {
        return $systemUser->cancelOrder($orderInfo);
    }
}

$customer = new Customer();
$driver = new Driver();
$order = new Order;

$orderInfo = [
    'driver_user_id' => 2,
    'user_id' => 1,
    'status' => 2
];
try {
    $driver->withdraw();
} catch (Exception $exception) {
    echo $exception->getMessage();
}

//$order->cancel($driver, $orderInfo);

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK