Help with PHP Classes.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Help with PHP Classes.

Post 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);
	}
}
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help with PHP Classes.

Post by Christopher »

Use autoloading of classes. See the manual.
(#10850)
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Re: Help with PHP Classes.

Post by psychotomus »

I don't understand how that could help me.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help with PHP Classes.

Post 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');
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Re: Help with PHP Classes.

Post by psychotomus »

Thanks AbraCadaver. I'll give these a try, but what exactly does Registry do?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help with PHP Classes.

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply