Page 1 of 1

Help with PHP Classes.

Posted: Tue Jan 18, 2011 12:44 am
by psychotomus
My class is working just fine. I just think it is poorly designed.
One problem I am having is I have to require my mysql.class.php file in every function instead of just once. How can I fix this problem?

Code: Select all

require_once('../classes/mysql.class.php');
$mysql = new mysql();

Code: Select all

<?php
class members
{
	
	//construct
	function __construct($action)
	{		
		if($_SESSION['userID'] > 1)
		{
			exit();
		}
					
		switch($action)
		{
			case "latest":
				$this->membersLatest();
				break;
			case "online":
				$this->membersOnline();
				break;
		}
	}

	//display template
	function display($sub)
	{
		//vars to replace in looper file
		$replacers = array("{REGDATE}", "{USERNAME}", "{LASTACTIVE}", "{ACTIVE}");

		//open template file
		$template = file_get_contents("template/index.htm");
		$first_sec = file_get_contents("template/members_main.htm");
		$last_sec = file_get_contents("template/members_looper.htm");
		
		for($x=0; $x<count($sub); $x++)
		{
			$sub_template .= str_replace($replacers, $sub[$x], $last_sec);
		}
	
		$sub_template = str_replace("{SUBCONTENT}", $sub_template, $first_sec);
		$template = str_replace("{CONTENT}", $sub_template, $template);
		
		echo $template;
	}
	
	//latest members
	function membersLatest()
	{

		require_once('../classes/mysql.class.php');
		$mysql = new mysql();
		
		//get latest regged members
		$mysql->query("SELECT userName, userRegDate, userLastActive, userActCode FROM user ORDER BY userID DESC");
		while($data = mysql_fetch_assoc($mysql->result))
		{
			//check if account activated
			if($data['userActCode'] == 0)
			{
				$active = "Yes";
			}
			else
			{
				$active = "No";
			}

			//set data array for loop in template
			$user[] = array(date("m/d/Y h:i a", $data['userRegDate']),$data['userName'],  date("m/d/Y h:i a", $data['userlastActive']), $active);
		} 
		
		$this->display($user);
	}
	
	//online users
	function membersOnline()
	{

		require_once('../classes/mysql.class.php');
		$mysql = new mysql();
		$time = time() - (60 * 10); //10 minutes
		
		//get online users
		$mysql->query("SELECT userName, userRegDate, userLastActive, userActCode FROM user WHERE userLastActive>'$time' ORDER BY userLastActive ASC");
		while($data = mysql_fetch_assoc($mysql->result))
		{
			//check if account activated
			if($data['userActCode'] == 0)
			{
				$active = "Yes";
			}
			else
			{
				$active = "No";
			}

			//set data array for loop in template
			$user[] = array($data['userName'], date("m/d/Y h:i a", $data['userRegDate']), date("m/d/Y h:i a", $data['userlastActive']), $active);
		} 
		
		$this->display($user);
	}
}
?>

Re: Help with PHP Classes.

Posted: Tue Jan 18, 2011 1:55 am
by Christopher
Use autoloading of classes. See the manual.

Re: Help with PHP Classes.

Posted: Tue Jan 18, 2011 2:19 pm
by psychotomus
I don't understand how that could help me.

Re: Help with PHP Classes.

Posted: Tue Jan 18, 2011 2:30 pm
by AbraCadaver
Set $mysql as an object var so it is contained within the object. Rough example:

Code: Select all

function __construct() {
   require_once('../classes/mysql.class.php');
   $this->mysql = new mysql();
}
// then use $this->mysql in other functions
Or you could instantiate mysql elsewhere and pass it in:

Code: Select all

require_once('../classes/mysql.class.php');
$mysql = new mysql();

Code: Select all

function __construct($mysql) {
   $this->mysql = $mysql;
}
// then use $this->mysql in other functions
Maybe preferred? Use a registry class:

Code: Select all

require_once('../classes/mysql.class.php');
$mysql = new mysql();
Registry::addObject('mysql', $mysql);

Code: Select all

function someFunction() {
   $mysql = Registry::getObject('mysql');
}

Re: Help with PHP Classes.

Posted: Wed Jan 19, 2011 1:03 am
by psychotomus
Thanks AbraCadaver. I'll give these a try, but what exactly does Registry do?

Re: Help with PHP Classes.

Posted: Wed Jan 19, 2011 7:26 am
by AbraCadaver
psychotomus wrote:Thanks AbraCadaver. I'll give these a try, but what exactly does Registry do?
Lots of examples out there. It stores stuff, mainly objects that you need to be available application wide.