gpt4 book ai didi

php - 在 OOP 中为网站推荐好友功能

转载 作者:行者123 更新时间:2023-12-04 05:53:34 26 4
gpt4 key购买 nike

我正在尝试掌握 OOP,只是想要一些关于如何解决问题的建议。

我正在我的网上商店建立一个推荐 friend 系统,所以当用户注册时,他们将输入推荐他们的 friend 的电子邮件地址。这个已经存在的 friend 然后在他们的在线帐户中获得一定数量的积分(货币)。

我已经使我的大部分系统只使用 PHP 中的函数和过程编程,但想知道是否有人可以指出正确的方向,使推荐成为 OOP 中的 friend 系统,或者即使它值得在 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");

关于php - 在 OOP 中为网站推荐好友功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9784014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com