我正在努力掌握OOP,只想得到一些关于如何处理问题的建议。
我正在我的网店上建立一个推荐朋友系统,所以当用户注册时,他们会输入推荐他们的朋友的电子邮件地址。然后,这个已经存在的朋友在他们的在线账户中获得一定数量的积分(货币)。
我的大部分系统都是使用PHP中的函数和过程编程,但我想知道是否有人能为我指明正确的方向,让引用成为OOP中的朋友系统,或者它是否值得在OOP中构建?
我确实理解OOP的基本原理,但不太确定如何从开始到结束使用它来完全构建程序。
好的,这就是我要做的
Class User {
// ....
public function newUser(array $params, $isRefer = false, $referal = null) {
// $params Parameters for new user, like name, address etc
// $isRefer boolean value to trigger referal
// $referal Email of the referer
$newUser = new user_model($params);
//Create a DBO of user, as per the new parameters
if(isReferal) { $referer = $this -> getUserIdByEmail($referal); }
//If is referal, get his id, based on email
$status = $this -> add($newUser); //Add the user
//Add the user
if($status) {
//Once it is success
$referer = new User($referer); //Create a object of referer
$referer -> addPoints(50); // Give bonus point
}
return true; //Indicate the success
}
public function __construct($id = null) {
if($id) {
//create the object
$this -> id = $id;
}
// ....
}
protected function getUserIdByEmail($email) {
//get the id
return $id;
}
protected function addPoints($points) {
//add the points on database table
return true;
}
}
DBO类可能类似于这个
class UserDBO {
protected $name;
protected $address;
public function __construct(array $params) {
$this -> name = $params['name'];
$this -> address = $params['address'];
}
}
用法:
$userObj = new User();
$userObj -> newUser(array("name", address"), true, "referal@email.com");