First Contact PHP abstract Factory mode (ELGG) _php tips

Source: Internet
Author: User
Want to achieve such a function: Launch a website invitation activity, then the participant (owner) will promote the website address link sends the friend, the friend clicks the link to register successfully after the website, owner's invitation log record bar number plus 1.
Activity class Activities
Copy Code code as follows:

Class Activity extends Elggentity {
Private $strategy; To save a policy instance
Public Function __construction ($guid) {
...
$this->load ($GUID); Loading entities
}
Public Function Addlog ($data) {
$this->strategy->addlog ($data); is actually Defaultactivitystrategy::addlog ($data)
}
Public function Load ($guid) {
if (Parent::load ($guid)) {//This procedure assigns all the properties of this instance to the database, so the value of the $this->strategyname is already assigned.
if ($this->strategyname!= ') {
$this->strategy = abstractactivitystrategy::getinstance ($this->strategyname); Load Policy class
}
return true;
}
return false;
}
}

Log Class Activitylog
Copy Code code as follows:

Class Activitylog extends Elggentity {
$private CountValue; Number of invitation Records
...
}

Policy class
Description: elggentity: All entity base classes. Abstractactivitystrategy: Active abstract class
First, create an activity:
Copy Code code as follows:

$activity = new activity ();
$activity->name = ' kknd '; Activity name
$activity->strategyname = ' DEFAULT '; Policy Name
$activity->save (); Save the active class to the database, and the newly added attributes (such as Strategyname) will also be saved

When someone receives an invitation, click on the link, Owner's invitation record entry +1
such as the invitation URL is http://www.xinwusi.com/KKND/1234
Where/kknd/is the active name and 1234 is the GUID of owner, assuming that the activity has a GUID of 8888, the
$activity = new activity (8888); Get active entity
$activity->addlog ($data); Add an invitation record. $data includes the GUID of owner, the GUID of the activity, the activity name, name, and so on.
The process of the last 2 lines of code reads the policy name of the active entity, generates a policy entity based on the policy name, saves it in its own $stragety attribute, and then invokes the Addlog method to increase the logging.
When there is a new activity, the active instance attribute's policy name is changed directly, and the method in the corresponding new policy can be invoked.
Copy Code code as follows:

Class Defaultactivitystrategy extends Abstractactivitystrategy {
...
Public Function Addlog ($data) {
$activityLog = new Activitylog ();
...
$activityLog->save ();
$activityLogAmount = new Activitylogamount (); Count 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.