True OOP =)

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
FounderSim
Forum Newbie
Posts: 8
Joined: Fri Feb 10, 2012 4:50 pm

True OOP =)

Post by FounderSim »

So many many years ago, someone once told me I could create an OOP script like this: I havn't learned any new technique's in many years with extend or implements. How can I convert these to that.


My INDEX

Code: Select all

<?php
session_start();
include 'includes/includes.php';

if(!empty($_GET['where']))
{
	if(is_file("classes/" . htmlentities($_GET['where']) . ".php"))
	{
		include "classes/" . htmlentities($_GET['where']) . ".php";
		$plugg = new plug($db, $smarty, $player);
		$smarty->assign("html_file", $plugg->page());
	}
	else
	{
		$smarty->assign("heading", "Page Not Found");
		$smarty->assign("html_file", "404");
	}
}
else
{
	//vars
	$smarty->assign("html_file", "intro");
}


$smarty->display("main.htm");

?>

a plugin file

Code: Select all

<?
include "includes/constants.php";
class plug
{
	//vars
	private $db;
	private $smarty;
	private $player;
	private $pager;

	public function __construct($dbRef, $smartyRef, $playerRef)
	{
		$this->db = $dbRef;
		$this->smarty = $smartyRef;
		$this->player = $playerRef;
		
		//if not logged in, redirect to login page
		if(!isset($_SESSION[$_SESSION['game'] . '_userID']))
		{
			header("Location: " . $_SESSION[$_SESSION['game'] . '_url'] . "/user/login/");
		}

		//see what we doing here. 
		//login, register, forgot password, or reset password
		switch ($_GET['action'])
		{
		
			case "online":
				$this->usersOnline();
				break;
			case "list":
				$this->usersList();
				break;
		}
		
	}

	public function usersOnline()
	{
		//get online time
		$user_online_time = time() - (10 * 60);
		
		//var
		$Data = array();

		//query to get all top leveled players
		$rows = $this->db->fetch_array("SELECT " . CHAR_TABLE . ".charID, " . CHAR_TABLE . ".charUserID, " . CHAR_TABLE . ".charLVL, " . USER_TABLE . ".userID, " . USER_TABLE . ".userHandle FROM " . CHAR_TABLE . ", " . USER_TABLE. " WHERE " . USER_TABLE . ".userID=" . CHAR_TABLE . ".charUserID AND " . USER_TABLE . ".userLastActive>='" . $user_online_time ."' AND " .  CHAR_TABLE . ".charGameRefID='" . $this->player->gameRefID . "'");

		//get all records
		foreach($rows as $rec)
		{
			//set data for template
			$Data[] = array("handle" => $rec['userHandle'], "lvl" => $rec['charLVL'], "char_id" => $rec['charID']);
		}

		//template vars
		$this->smarty->assign("looper", $Data);
		$this->pager = "users_online";		
	}

	//display news
	public function usersList()
	{
		
		//var
		$Data = array();

		//query to get all top leveled players
		$rows = $this->db->fetch_array("SELECT " . CHAR_TABLE . ".charID, " . CHAR_TABLE . ".charUserID, " . CHAR_TABLE . ".charLVL, " . USER_TABLE . ".userID, " . USER_TABLE . ".userHandle FROM " . CHAR_TABLE . ", " . USER_TABLE. " WHERE " . USER_TABLE . ".userID=" . CHAR_TABLE . ".charUserID AND " .  CHAR_TABLE . ".charGameRefID='" . $this->player->gameRefID . "'");

		//get all records
		foreach($rows as $rec)
		{
			//set data for template
			$Data[] = array("handle" => $rec['userHandle'], "lvl" => $rec['charLVL'], "char_id" => $rec['charID']);
		}

		//template vars
		$this->smarty->assign("looper", $Data);
		$this->pager = "users_list";
	}
	
	public function page()
	{
		return $this->pager;
	}
	
	//get top 10 players by level
	public function siteTopLvledPlayers()
	{

		//var
		$Data = array();
		$count = 1;

		//query to get all tasks that player can do..
		$rows = $this->db->fetch_array("SELECT " . CHAR_TABLE . ".charID, " . CHAR_TABLE . ".charUserID, " . CHAR_TABLE . ".charLVL, " . USER_TABLE . ".userID, " . USER_TABLE . ".userHandle FROM " . CHAR_TABLE . ", " . USER_TABLE. " WHERE " . USER_TABLE . ".userID=" . CHAR_TABLE . ".charUserID AND " .  CHAR_TABLE . ".charGameRefID='" . $this->player->gameRefID . "' ORDER BY " . CHAR_TABLE . ".charLVL DESC LIMIT 10");

		//get all records
		foreach($rows as $rec)
		{
			//set data for template
			$Data[] = array("rank" => $count, "handle" => $rec['userHandle'], "level" => $rec['charLVL'], "char_id" => $rec['charID']);
			$count++;
		}
		
		//template vars
		$this->smarty->assign("looper", $Data);
	}
}
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: True OOP =)

Post by Christopher »

It looks like your index.php is a Front Controller, your plug classes are Models and you are using Smarty as your View. That is pretty basic MVC and has the important separations that you should have in an application.

Modern MVC systems add and Action Controller that is loaded by the Front Controller in the way that you load your Model classes (with the plug class in them). Typically the Action Controller class has the same name as the file (sometimes with "Controller" added) so the file foo.php would have the class foo or FooController or similar. This allows you to use multiple Model classes per Action Controller which will allow you to reduce code duplication.
(#10850)
FounderSim
Forum Newbie
Posts: 8
Joined: Fri Feb 10, 2012 4:50 pm

Re: True OOP =)

Post by FounderSim »

So how can I rid of passing paramaters and use extends and/or implements?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: True OOP =)

Post by Christopher »

FounderSim wrote:So how can I rid of passing paramaters
What parameters do you want to get rid of?
FounderSim wrote: and use extends and/or implements?
You start doing that by creating base classes. But you may need to restructure first. The way you have it now, your Models are specific to a request and don't actually model the domain.
(#10850)
Post Reply