I've seen a dozen of topics and questions that are stating to use ORM in PHP (30 php best practices for beginners and Why use an orm in php)
So I'm wondering, HOW to use ORM with PHP. Especially OOPHP.
Let's say we've a class User and we are registering one. (I'm using RedBean)
<?php
class User {
private $username;
private $password;
public function registerUser($username, $password) {
require $root . "classes/Helper.php";
$helper = new Helper();
$salt = sha1($helper->generateSalt());
$this->username = $username;
$this->password = sha1($password + $salt);
$this->salt = $salt;
$this->store();
}
private function store() {
$user = R::dispense(__CLASS__);
$user->username = $this->username;
$user->password = $this->password;
}
}
?>
This works. Everytime a user gets registered, I'm storing it aswell.
Or is it maybe better to put the store()
function in the __construct()
Or is there an even better way to do it?
Is it usefull to use __CLASS__
or should I better use 'User'
?
private function load($username){
$user = R::find(__CLASS__, 'username=?', array($username));
$this->username = $user->username;
$this->password = $user->password;
$this->salt = $user->salt;
$this->email = $user->email;
}
Now if I want to load a user I can in my page create a new User object and call load()
to fill it with the desired user.
Or should I directly use $userbean = R::find('User', 'username=?', array($username));
and put it in a $user = new User();
?
I'm getting a little confused by the ways in which several things can be done. Is this actually made to use with object-oriented PHP?
[edit:] So, is my approach this way in using RedBean and OOPHP right?