PHP100 2019-03-27
代码如下:
class Activity extends ElggEntity { private $strategy; //用于保存策略实例 public function __construction($guid) { ... $this->load($guid); //载入实体 } public function addLog($data) { $this->strategy->addLog($data); //实际是 DEFAULTActivityStrategy::addLog($data) } public function load ($guid) { if (parent::load($guid)) { //此过程会将本实例的所有属性从数据库中赋值,因此 $this->strategyName的值已经被赋上了。 if ($this->strategyName != '') { $this->strategy = AbstractActivityStrategy::getInstance($this->strategyName); //加载策略类 } return true; } return false; } }
代码如下:
class ActivityLog extends ElggEntity { $private countValue; //邀请记录数 ... }
代码如下:
$activity = new Activity(); $activity->name = 'KKND'; //活动名称 $activity->strategyName = 'DEFAULT'; //策略名称 $activity->save(); //将活动类保存至数据库,新添加的属性(比如strategyName)也会保存
代码如下:
class DEFAULTActivityStrategy extends AbstractActivityStrategy { ... public function addLog($data) { $activityLog = new ActivityLog(); ... $activityLog->save(); $activityLogAmount = new ActivityLogAmount(); //计数类 ... $activityLogAmount->countValue += 1; $activityLogAmount->save(); } }