以下是一个简单的PHP实例,演示如何处理库存改变的操作。在这个例子中,我们将创建一个简单的库存管理系统,包括增加和减少库存的功能。
```php

// 假设我们有一个商品类
class Product {
public $id;
public $name;
public $stock;
public function __construct($id, $name, $stock) {
$this->id = $id;
$this->name = $name;
$this->stock = $stock;
}
// 增加库存
public function addStock($quantity) {
$this->stock += $quantity;
return $this->stock;
}
// 减少库存
public function reduceStock($quantity) {
if ($this->stock >= $quantity) {
$this->stock -= $quantity;
return $this->stock;
} else {
return false; // 库存不足
}
}
}
// 创建一个商品实例
$product = new Product(1, "


