Home  >  Article  >  Backend Development  >  First contact with php abstract factory pattern (Elgg)_PHP tutorial

First contact with php abstract factory pattern (Elgg)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:39:501129browse

I want to implement such a function: launch a website invitation activity, and then the participant (owner) sends the promoted website address link to a friend. After the friend clicks the link, he successfully registers on the website, and the number of owner's invitation log records is increased by 1.
Activity class Activity

Copy code The code is as follows:

class Activity extends ElggEntity {
private $strategy; //Used to save strategy instances
public function __construction($guid) {
...
$this->load($guid); //Load entities
}
public function addLog($data) {
$this->strategy->addLog($data); //actually DEFAULTActivityStrategy::addLog($data)
}
public function load ($ guid) {
if (parent::load($guid)) { //This process will assign all attributes of this instance from the database, so the value of $this->strategyName has already been assigned.
if ($this->strategyName != '') {
$this->strategy = AbstractActivityStrategy::getInstance($this->strategyName); //Load strategy class
}
return true;
}
return false;
}
}

Log class ActivityLog
Copy code The code is as follows:

class ActivityLog extends ElggEntity {
$private countValue; //Number of invitation records
...
}

Strategy class
Description: ElggEntity: base class for all entities. AbstractActivityStrategy: Activity abstract class
) First create an activity:
Copy code The code is as follows:

$activity = new Activity();
$activity->name = 'KKND'; //Activity name
$activity->strategyName = 'DEFAULT'; //Strategy name
$activity->save( ); //Save the activity class to the database, and the newly added attributes (such as strategyName) will also be saved

) When others receive the invitation and click the link, the owner's invitation record entry +1
For example, the invitation URL is http://www.xinwusi.com/KKND/1234
where /KKND/ is the activity name, 1234 is the owner's guid, assuming the activity's guid is 8888, then
$activity = new Activity(8888); //Get the activity entity
$activity->addLog($data); //Add invitation record. $data includes owner's GUID, activity's GUID, activity name, etc.
The process of the last two lines of code is to read the strategy name of the active entity, generate a strategy entity based on this strategy name, save it in its own $stragety attribute, and then call the addLog method to add log records.
When there is a new activity in the future, you can directly change the strategy name of the activity instance attribute, and then you can call the method corresponding to the new strategy.
Copy code The code is as follows:

class DEFAULTActivityStrategy extends AbstractActivityStrategy {
...
public function addLog( $data) {
$activityLog = new ActivityLog();
...
$activityLog->save();
$activityLogAmount = new ActivityLogAmount(); //Counting class
...
$activityLogAmount->countValue += 1;
$activityLogAmount->save();
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321487.htmlTechArticleI want to implement such a function: launch a website invitation event, and then the participant (owner) will link the promoted website address Send it to a friend. After the friend clicks the link, he successfully registers on the website. The owner’s...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn