PDA

View Full Version : PHP OOP and Classes


Troy297
04-15-2008, 01:53 AM
Right now I'm trying to build a code base for my newest project (Radio DJ Panel v4) and I want to do it using Classes, Objects, and with the OOP methodology. Problem is, I've never used any sort of Classes or OOP structure before....

So the issue that I'm having is with having the classes use each other. For instance, I have my DB class which deals with all MySQL related stuff and then I also have my Sessions class which, obviously, deals with all my Session/Cookie related stuff. What I'm trying to do is make my Session validation more secure so I am storing the Session ID's for any active Sessions in a DB and then referencing the DB whenever I create or use/interact with any existing Sessions.

I currently have it setup so that my "init.php" file requires all the different classes and then creates instances of each for use in my script. Now the issue I'm having is with how I would go about using the DB class inside the Session class. Should I create a new instance of the DB class inside the Session class to be able to use the DB class or how would you recommend doing it?

Here are some snippets from my code so far....

init.php

<?php
require('config.php');
require('classes/mysql.php');
require('classes/session.php');
$db = new mysql_db;
$ses = new session;
?>


session.php
<?php
class session{
public $session_id;
public $host;
public $browser;
public function __construct(){
$this->session_id = session_id();
$this->host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$this->browser = $HTTP_SERVER_VARS['HTTP_USER_AGENT'];
// This is where I need the DB query....
$num = $db->count("SELECT * FROM `sessions` WHERE `sessionid` = '".$this->session_id."' AND `host` = '".$this->host."' AND `browser` = '".$this->browser."'");
// The above code currently gives the following error:
// Fatal error: Call to a member function count() on a non-object....
if($num >= 1){
echo 'exists';
}else{
session_start();
}
}
}
?>

Note that these classes and whatnot are nowhere near finished yet but I just need some suggestions as to how I should do it.

Any help is greatly appreciated!

kbluhm
04-15-2008, 03:50 AM
$db does not exist within the session class.

You have to instantiate it as a property within the class, pass the instance to the class, or reference it through $GLOBALS. I'd not use the third option as it is bad practice and could become a headache should you rename the variable. You never know...

I'd instantiate it outside the class and import it into the class. Assign it to a property of:

private $db;

...then reference it as:

$num = $this->db->count( /* ... */ );

GJay
04-15-2008, 08:04 AM
Yeah, either pass it in via the constructor, or via a specific setter.

You might also want to do a search for the "registry pattern", as that can help a lot when you have lots of things that you use in lots of different places

aedrin
04-15-2008, 03:20 PM
Yeah, either pass it in via the constructor, or via a specific setter.

You might also want to do a search for the "registry pattern", as that can help a lot when you have lots of things that you use in lots of different places

Some people also suggest that you could combine the two efforts. To where you pass in a Registry object in the constructor.

Though, passing in the required objects separately is clearer and adds some visual dependancy requirements.