以下是一个简单的PHP对象模型实例,我们将创建一个购物车系统,其中包含商品类、购物车类和订单类。

商品类(Product.php)

```php

class Product {

public $id;

public $name;

public $price;

public function __construct($id, $name, $price) {

$this->id = $id;

$this->name = $name;

$this->price = $price;

}

public function getTotalPrice() {

return $this->price;

}

}

>

```

购物车类(Cart.php)

```php

class Cart {

private $products = [];

public function addProduct(Product $product) {

$this->products[$product->id] = $product;

}

public function removeProduct($id) {

unset($this->products[$id]);

}

public function getTotalPrice() {

$total = 0;

foreach ($this->products as $product) {

$total += $product->getTotalPrice();

}

return $total;

}

public function getProducts() {

return $this->products;

}

}

>

```

订单类(Order.php)

```php

class Order {

private $cart;

private $date;

public function __construct(Cart $cart) {

$this->cart = $cart;

$this->date = date('Y-m-d H:i:s');

}

public function getTotalPrice() {

return $this->cart->getTotalPrice();

}

public function getProducts() {

return $this->cart->getProducts();

}

public function getDate() {

return $this->date;

}

}

>

```

使用示例

```php

include 'Product.php';

include 'Cart.php';

include 'Order.php';

// 创建商品实例

$product1 = new Product(1, 'Laptop', 1000);

$product2 = new Product(2, 'Smartphone', 500);

// 创建购物车实例

$cart = new Cart();

$cart->addProduct($product1);

$cart->addProduct($product2);

// 创建订单实例

$order = new Order($cart);

// 输出订单信息

echo "