True OOP =)
Posted: Sat Apr 20, 2013 11:21 am
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
a plugin file
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);
}
}
?>