Initial exposure to the php abstract factory model (Elgg)

Source: Internet
Author: User
Tags elgg

To enable this function, the participant (owner) will send the promotional website address link to a friend. After clicking the link, the friend will successfully register the website, the number of invitation log records of the owner plus 1.
Activity
Copy codeThe Code is as follows:
Class Activity extends ElggEntity {
Private $ strategy; // used to save the policy instance
Public function _ construction ($ guid ){
...
$ This-> load ($ guid); // load an object
}
Public function addLog ($ data ){
$ This-> strategy-> addLog ($ data); // DEFAULTActivityStrategy: addLog ($ data)
}
Public function load ($ guid ){
If (parent: load ($ guid) {// This process will assign values to all attributes of this instance from the database, therefore, the value of $ this-> strategyName has been assigned.
If ($ this-> strategyName! = ''){
$ This-> strategy = AbstractActivityStrategy: getInstance ($ this-> strategyName); // load the policy class
}
Return true;
}
Return false;
}
}

Log ActivityLog
Copy codeThe Code is as follows:
Class ActivityLog extends ElggEntity {
$ Private countValue; // number of invited records
...
}

Strategy
Description: ElggEntity: All entity base classes. AbstractActivityStrategy: Activity abstract class
First, create an activity:
Copy codeThe Code is as follows:
$ Activity = new Activity ();
$ Activity-> name = 'knd'; // activity name
$ Activity-> strategyName = 'default'; // Policy Name
$ Activity-> save (); // save the activity class to the database. Newly Added attributes (such as strategyName) are also saved.

) When someone else receives the invitation and clicks the link, the owner's invitation record entries + 1
For example, the invitation URL is a http://www.xinwusi.com/KKND/1234
Here,/knd/is the activity name and 1234 is the owner's guid. If the guid of the activity is 8888
$ Activity = new Activity (8888); // obtain the activity entity
$ Activity-> addLog ($ data); // Add an invitation record. $ Data includes the owner's guid, activity guid, and activity name.
The last two lines of code read the Policy Name of the active entity and generate a policy entity based on the Policy Name, Which is saved in its $ stragety attribute, call the addLog method to add logs.
When there is a new activity in the future, you can directly change the Policy Name of the activity instance attribute to call the method corresponding to the new policy.
Copy codeThe Code is as follows:
Class DEFAULTActivityStrategy extends actactivitystrategy {
...
Public function addLog ($ data ){
$ ActivityLog = new ActivityLog ();
...
$ ActivityLog-> save ();
$ ActivityLogAmount = new ActivityLogAmount (); // counting class
...
$ ActivityLogAmount-> countValue + = 1;
$ ActivityLogAmount-> save ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.