1 to many relationship in OOP PHP in code?

2019-09-17 05:03发布

问题:

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

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
    }
}

回答1:

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
}