garry Posted June 20, 2008 Share Posted June 20, 2008 I'm trying to learn how to use classes so I decided to try turn the way I handle users into classes. Firstly, I was just starting by trying to return the amount of users online but I'm having some trouble. Can anyone help? Here's the code I'm using. The class code: <?php class user { public $usersonline; function users_online() { $this->mysqli_conn(); $query = 'SELECT * FROM usersonline'; $result = $mysqli->query($query); $this->usersonline = $result->num_rows; return $usersonline; } } ?> and when i try to initiate it: <?php $user = new user; $usersonline = $user->usersonline; echo $usersonline; ?> I've got no idea what I'm doing wrong and I've tried googling but haven't come up with much so any help would be appreciated. I'd also like it if you could tell me if I'm using classes in the same way or point out some suggestions on how I could use them better for my users. Thankyou! Link to comment https://forums.phpfreaks.com/topic/111044-classes/ Share on other sites More sharing options...
Stephen Posted June 20, 2008 Share Posted June 20, 2008 Try doing <?php $user = new user; $usersonline = $user->users_online(); echo $usersonline; ?> Link to comment https://forums.phpfreaks.com/topic/111044-classes/#findComment-569831 Share on other sites More sharing options...
DarkWater Posted June 20, 2008 Share Posted June 20, 2008 The purpose of OOP is not to completely recreate procedural style PHP, as you just did. You need to break it up into small, meaningful classes that each do their own thing. Ex: A Database class, a User class, a Session class, etc. They all incorporate each other, yet function separately. Also, you use visibility keywords on functions too, such as: public function users_online() { } Link to comment https://forums.phpfreaks.com/topic/111044-classes/#findComment-569834 Share on other sites More sharing options...
garry Posted June 20, 2008 Author Share Posted June 20, 2008 Stephen - your approach gives me an error saying that $mysqli is not defined. How can I have access to the database in that function with my function called mysqli_conn();. Darkwater - that is what I'm trying to do, is it not? You said break them up into small classes such as a user class. That's what I'm trying to do - put all of these functions into one user class. Or am I not understand you correctly? And how would I go about making a class for the database when I've already got this function that I use: function mysqli_conn() { global $mysqli; $host = "localhost"; $user = "xxxxx"; $pass = "xxxxx"; $db = "dev"; $mysqli = new mysqli($host, $user, $pass, $db); if (!$mysqli) { die ('MySQL connection could not be established.'); } return $mysqli; } Link to comment https://forums.phpfreaks.com/topic/111044-classes/#findComment-569837 Share on other sites More sharing options...
DarkWater Posted June 20, 2008 Share Posted June 20, 2008 You never want to connect to a database inside of a class function other than inside a Database class. By Database class I mean a singleton class for use among various classes. I.E: /* Database class */ class DB { protected static $connection; public static function getInstance() { if (!self::$connection) { self::$connection = mysqli_connect('localhost', 'test', 'test'); return self::$connection; } else { return self::$connection; } } Then: class User { protected $connection; protected $userinfo; public function __construct() { $this->connection = DB::getInstance(); } public function retrieveInfo($userid) { $this->userinfo = new Userinfo($userid); } public function getInfo() { return $this->userinfo; } } Then...: class Userinfo { protected $info; public function __construct($userid) { //get user stuff or something $this->info = $userdata; } } Then you'd have some functions in Userinfo to manipulate the userinfo object. Link to comment https://forums.phpfreaks.com/topic/111044-classes/#findComment-569840 Share on other sites More sharing options...
bluejay002 Posted June 20, 2008 Share Posted June 20, 2008 nice class layout. __construct() is a constructor. before, it uses the name of the class as the constructor. as of PHP 5, even if you do not use this, it will look for the old way of specifying constructors for backward compatibility reasons. but if you are using earlier than PHP 5... i advise you change to PHP 5 or else, use the other way of specifying constructors. Link to comment https://forums.phpfreaks.com/topic/111044-classes/#findComment-569846 Share on other sites More sharing options...
garry Posted June 20, 2008 Author Share Posted June 20, 2008 bluejay - it's all good, I'm using PHP5 and any server i'll be using this on will also be using that. darkwater - thanks for your help so far! So I've altered your database class a bit first to see if I can just use it to connect to the database but I'm getting some errors Here's the new database class: <?php class database { protected static $connection; public static function getInstance() { $host = "localhost"; $user = "xxxxx"; $pass = "xxxxx"; $db = "dev"; if (!self::$connection) { self::$connection = new mysqli($host, $user, $pass, $db); return self::$connection; } else { return self::$connection; } } } ?> and here's how I am trying to initiate a connection (I think i'm doing it wrong though!) <?php $conn = new database::getInstance(); ?> I get the following error: Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' in /htdocs/dev/index.php on line 10 Line 10 is the $conn bit Link to comment https://forums.phpfreaks.com/topic/111044-classes/#findComment-569848 Share on other sites More sharing options...
DarkWater Posted June 20, 2008 Share Posted June 20, 2008 You don't use 'new' there. That static function returns a single instance that is reused across the script between each getInstance call. Link to comment https://forums.phpfreaks.com/topic/111044-classes/#findComment-569851 Share on other sites More sharing options...
garry Posted June 20, 2008 Author Share Posted June 20, 2008 Still trying to understand this fully.. Here's the class you showed me before: class user { protected $connection; // protected $userinfo; public function __construct() { $this->connection = database::getInstance(); } public function retrieveInfo($userid) { $this->userinfo = new Userinfo($userid); } public function getInfo() { return $this->userinfo; } } class userinfo { protected $userinfo; public function __construct($userid) { //get user stuff or something $this->userinfo = $userdata; } } How exactly would I go about getting the user info? I just use a query inside the __construct function? or is this not correct? And if this is true, once the function is created, if I want to get a users username, do I just have to put something like this in the code: $user = new user; $username = $user->userinfo->username; ? Link to comment https://forums.phpfreaks.com/topic/111044-classes/#findComment-570004 Share on other sites More sharing options...
garry Posted June 20, 2008 Author Share Posted June 20, 2008 anyone? Link to comment https://forums.phpfreaks.com/topic/111044-classes/#findComment-570055 Share on other sites More sharing options...
DarkWater Posted June 20, 2008 Share Posted June 20, 2008 Okay, well, first of all, you commented out the fourth line...I don't know why. And yes, in the constructor, of the Userinfo class is where I'd populate the info variable inside the Userinfo instance. But, you can't manipulate the userinfo like this: $username = $user->userinfo->username; That's because $userinfo inside User is protected. You'll need to return the instance of the Userinfo object and create some class methods to manipulate the data. $user = new User(); $user->retrieveInfo(22); $uinfo = $user->getInfo(); echo "Your username is " . $uinfo->getUsername(); But obviously you'll need a getUsername method. Link to comment https://forums.phpfreaks.com/topic/111044-classes/#findComment-570328 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.