Re: Habbo4.1 [PHP5, OOP, TPL, MySQLi]
Am I?
Verstuurd vanaf mijn GT-I8260 met Tapatalk
Re: Habbo4.1 [PHP5, OOP, TPL, MySQLi]
Quote:
Originally Posted by
Johnix1337
Hey.
I want to give you some tips with your code.
PHP Code:
class User{
public function database($h, $u, $p, $db){
$this->conn = new mysqli($h,$u,$p,$db); // So i don't have to use global.
}
public function grabData($k) {
$q = $this->conn->query("SELECT * FROM users WHERE username = '$this->session'");
$r = mysqli_fetch_assoc($q);
return $r[$k];
}
}
The grabData() method is pretty inefficient. I would store the result which is given in a private attribute, so it doesn't have to run the query everytime grabData is called.
Another tip is, that you use dependency injection with the database. The method database() will create an mysqli instance everytime when you use the User object.
I would pass the Database as a parameter so the database() method looks a bit like this.
PHP Code:
class User{
/* ... */
public function database(MySQLi $connection)
{
this->conn = $connection;
}
And if you want to call this function, you simply do this:
PHP Code:
$connection = new MySQLi('bla','bla','bla','bla');
$user = new User('blabla');
$user->database($connection);
I would put the $connection variable in the core file, so you only use one instance of it.
I've rewritten the User class with minor adjustments:
PHP Code:
class User {
private $row = null;
public function __construct($session) {
$this->session = $session;
}
public function database($connection){
$this->conn = $connection; // Dependency Injection, maybe put this in the constructor
}
public function grabData($k) {
if (is_null($this->row)){
$q = $this->conn->query("SELECT * FROM users WHERE username = '$this->session'");
$this->row = mysqli_fetch_assoc($q);
}
return $this->row[$k];
}
// Call RefreshRow() if something has been updated in the database
public function RefreshRow()
{
$q = $this->conn->query("SELECT * FROM users WHERE username = '$this->session'");
$this->row = mysqli_fetch_assoc($q);
}
}
I didnt't test anything so forgive me if I made any mistakes.
Hope I could help!
Why do you use object oriented style and procedural style at the same time?
Re: Habbo4.1 [PHP5, OOP, TPL, MySQLi]
Quote:
Originally Posted by
Jerking
Why do you use object oriented style and procedural style at the same time?
It's still in development, i made some minor mistakes, I know
Verstuurd vanaf mijn GT-I8260 met Tapatalk
Re: Habbo4.1 [PHP5, OOP, TPL, MySQLi]
Re: Habbo4.1 [PHP5, OOP, TPL, MySQLi]
Nice, just for me it looks a bit empty.
Re: Habbo4.1 [PHP5, OOP, TPL, MySQLi]
Quote:
Originally Posted by
Finley7
I used it on earlier projects. I wanted to create my own engine now.:thumbup1:
I don't know why, but register doesn't work... :\ Can you help me?