Jump to content

classes..


garry

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.