在PHP中的OOP中有1到许多关系? [英] 1 to many relationship in OOP PHP in code?

查看:95
本文介绍了在PHP中的OOP中有1到许多关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是实现的类图,但是代码中的一对多关系会是什么样子?

This is the class diagram implemented, but what would a 1-to-many relationship look like in code?

这是一段文字,请stackoverflow接受我的问题

This is a text please stackoverflow accept my question

class Customer {

    public $name;
    public $location;

    public function sendOrder(){
            //method here
    }

    public function receiveOrder(){
            //method here
    }

}

class Order {
    public $date;
    public $number;

    public function sendOrder(){
            //method here
    }

        public function receiveOrder(){
            //method here
        }
    }

class SpecialOrder Extends Order{
    public function dispatch(){
        //method here
    }
}

class NormalOrder Extends Order{
    public function dispatch(){
        //method here
    }

    public function receive(){
        //method here
    }
}


推荐答案

类图不会强制实现多重性。因此,您可以自由执行它。最简单的转发方式是在 Customer 内的 Order 数组。我的PHP很生锈,但看起来可能像这样:

The class diagram does not force any implementation for multiplicity. So you are free on how to implement it. The easiest most forward is an array of Order inside Customer. My PHP is quite rusted, but it would probably look like this:

class Customer {
  $orders = []; // references to orders
  // rest of the class
}

这篇关于在PHP中的OOP中有1到许多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆