Hi all,
I've literally JUST dived into OOP and it's a totally different world. I've written some pretty awful code just to grasp OOP basics. The following code is all in the same file and runes fine, there are no warnings, errors, notices and so forth yet it doesn't work correctly... I think it's something dead simple and if it is I promise to facepalm as hard as I can.
Thanks for any help in advance.
Code:
<?php
error_reporting(E_ALL);
class database{
protected $_link, $_result, $_numRows;
public function __construct(){
$this->_link = mysqli_connect('localhost', 'root', '', 'studybubble') or die(mysqli_error());
}
public function disconnect(){
mysqli_close($this->_link);
}
public function query($string){
$this->_result = mysqli_query($this->_link, $string);
$this->_numRows = mysqli_num_rows($this->_result);
}
public function numRows(){
return $this->numRows();
}
}
class user{
private $firstname;
private $lastname;
private $avatar;
function __construct(){
$db = new database;
$stm = $db->query("SELECT fn, ln, avatar FROM `users` WHERE `authuser` = 'mmcdonald'");
$this->firstname = $stm['fn'];
$this->lastname = $stm['ln'];
$this->avatar = $stm['avatar'];
}
public function firstname(){
return $this->firstname;
}
public function lastname(){
return $this->lastname;
}
public function avatar(){
return $this->avatar;
}
}
$user = new user;
echo 'Firstname: '.$user->firstname().'<br />
Lastname: '.$user->lastname().'<br />
Avatar: '.$user->avatar();
?>
The current output:
Firstname:
Lastname:
Avatar:
Again PLEASE ignore the security aspects and optimisation aspetcs and such, they're not my focus in this exercise :) Thanks!