ArcEmu: [Tutorial #5]: Oopin' - ArcEmu

Jump to content

Toggle shoutbox Lastest Announcements

dfighter  : (07 December 2014 - 12:06 PM) Arcemu is in hibernation mode, please read http://arcemu.org/fo...showtopic=26903
dfighter  : (01 January 2013 - 05:56 PM) Arcemu wishes you all a happy new year!
Hasbro  : (12 September 2012 - 10:01 AM) Please excuse our outage from the web! Our web host had a major malfunction!
dfighter  : (01 September 2012 - 04:05 PM) Since the spam bots just don't want to stop, I've enabled admin verification when registering.
dfighter  : (23 January 2012 - 09:56 PM) Please note that from now on you will need to confirm your email on the wiki in order to edit it!
Hasbro  : (31 December 2011 - 12:50 PM) Happy New Years all!
Navid  : (26 December 2011 - 04:09 AM) Merry Christmas !!!!!! Happy holidays all :)
WAmadeus  : (24 December 2011 - 03:54 PM) Merry Christmas to all!
dfighter  : (24 December 2011 - 11:05 AM) The Arcemu team wishes y'all a Merry Christmukkah!
Hasbro  : (05 October 2011 - 12:53 PM) Looking for web designers for upcoming web related project. If you're interested in designing user interfaces contact me
dfighter  : (02 September 2011 - 03:47 PM) So who here wants vehicles in Arcemu? :P http://arcemu.org/fo...showtopic=25440
Hasbro  : (14 August 2011 - 03:25 PM) Join us on irc, grab an irc client and connect to irc.freenode.net join channel #arcemu /server irc.freenode.net:6667 /join #arcemu
jackpoz  : (03 August 2011 - 05:33 AM) to all Lua Engine (old one) users: please check http://arcemu.org/fo...showtopic=25274
Hasbro  : (20 May 2011 - 05:27 PM) Looking for people experienced with CMake configuration and setup! Contact me asap
Hasbro  : (15 May 2011 - 05:03 PM) ArcEmu is recruiting C++ programmers, contact Hasbro if interested.
paroxysm  : (03 May 2011 - 06:26 PM) Updated luabridge gossip example to describe the whole gossip creation process rather than just how to create menu. Gossip tutorial
paroxysm  : (23 April 2011 - 11:35 AM) Lua writers can refer to the Luabridge Tutorials section in the Wiki to learn how to write gossip code correctly.
Hasbro  : (20 April 2011 - 05:22 PM) Thank you for your continuous contribution of bug reports, we are working on them.
Hasbro  : (17 April 2011 - 03:20 AM) Please consider donating to support our bills. Donations can be sent using PayPal to donations@arcemu.org - Thank you for your support.
paroxysm  : (10 April 2011 - 12:43 AM) Refer to the Luabridge Tutorials section in the Wiki to learn the new syntax of luabridge.
Resize Shouts Area

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[Tutorial #5]: Oopin' Think you can handle it?

#1 User is offline   Bob Herman 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 452
  • Joined: 11-October 08
  • Gender:Male

Posted 18 September 2009 - 06:37 PM

Intro

Welcome to the 5th tutorial of Object Oriented Programming (OOP!). DISCLAIMER THINGY: We won't be making anything new n' cool during this tut, just learning some good php. We have a lot of stuff to cover for this, because this is 'professional' php.

Something people ask a lot about OOP is, why use it? The answer is simple. OOP allows you to structure your php much more efficiently. Using it you can keep Libraries - where all the php does the actions - and the Web Files - that display everything. OOP also can cut down the size of a file if you have repetition.

The Basics

First I want you to show you two things.

<?php
$test = false;
if(!$test)
     echo 'No test';
elseif($test)
     echo 'There is a test';
?>


This will echo No test.

<?php
$test = true;
if(!$test)
     echo 'No test';
elseif($test)
     echo 'There is a test';
?>



This will echo There is a test.

So in short, false returns as no for a variable and true returns as yes for a variable. This might not seem like much, but a lot of OOP is based off false/true stuff.

The second thing I want to show you are class and functions. These play a HUGE role in OOP. This is a function:

function test()
{
     echo 'test';
}


You can call up this function using:

<?php
function test()
{
     echo 'test';
}
test();
?>


I can also make the function echo what I want it to using this:

<?php
function test($somevar)
{
     echo $somevar;
}
test("test");
?>


And that's it for the basics. :)

Starting out with OOP

Classes is the real meat of OOP. This is where you store your functions. So let's make a class.

<?php
class test
{
    //FUNCTIONS HERE
}
?>


Now, let's add in our function from the basics section.

class test
{
	function test($somevar= '') //notice that for classes the function is slightly altered
	{
	     echo $somevar;
	}
}


Now calling up the function is different. You actually need to make a variable for it. The format is: $<name of variable> = new <name of class>;

$class = new test;


Then, to call up the function you do $"variable name"->"function name"();

$class = new test;
$class->test();


Put all of that together, and you get:

<?php
class test
{
	function test($somevar= '')
	{
	     echo $somevar;
	}
}
$class = new test;
$class->test("testers");
?>


With variables it's a bit different. The geniuses over at PHP decided that with classes variables should be different from variables from 'normal' non-oop php.

var test; // This variable acts like a normal variable in PHP.
public test2; //This variable can be accessed by ANYTHING
protected test3; //This variable can only be accessed by the file it is in
private test4; //This variable can only be accessed by the class it is in (I think)


So going back to our test script, you can add some variables.

<?php
class test
{
    var $who = 'yo momma';
	function test($somevar= '')
	{
	     echo $somevar;
	}
}
$class = new test;
$class->test("testers");
?>


And of course, calling them up is different.

<?php
class test
{
    var $who = 'yo momma';
	function test($somevar= '')
	{
	     echo $somevar;
	}
}
$class = new test;
$class->test("testers");
echo $class->who; //notice there isn't the $ sign!
?>


There ya go, your first oop script. :D

Going on (Part 1)

Now obviously, if OOP is advanced PHP its gotta be harder than that. I'm going to do a simple connect to server/database script to show you the finer points of OOP.

Let's make our class, and let's load it.

<?php
class mysql
{
}

$mysql = new mysql;


Now we're going to add a __construct function, which is automatically loaded with the class. Like calling any other function, the __construct may have variables entered into it. I want to have my server connecte,d and I want it to check if I want a debug report (which we will make).

<?php
class mysql
{
     function __construct($host = '', $user = '', $pass,  $debug = false)
     {
     }
}

$mysql = new mysql('127.0.0.1', 'root', 'root', true);


Let's start with the debugger. First let's make a checker to see if we want to load the debugger. We only want the debugger to load if we tell it to!

<?php
class mysql
{
     function __construct($host = '', $user = '', $pass,  $debug = false)
     {
     if($debug)
          $this->enableDebug();
     }
}

$mysql = new mysql('127.0.0.1', 'root', 'root', true);


What we're going to do is make a variable that turns on/off the debugger. The enableDebug simply will set the debug variable ($debugger) to true.

<?php
class mysql
{
     function __construct($host = '', $user = '', $pass,  $debug = false)
     {
     if($debug)
          $this->enableDebug();
     }

     public $debugger = false;
     public function enableDebug()
     {
          $this->debugger = true;
          $this->debug('Starting Debugger');
     }
}

$mysql = new mysql('127.0.0.1', 'root', 'root', true);


Now I've turned on, and sent my first debug to my non-existant debugger. Now the debugger itself is going to be a private function (since we don't want people to mess with it), but it will store information in a variable, which will be public. Also, for the sake of fanciness, I add a timer on the debugger. It makes it seem much more professional :P

<?php
class mysql
{
     function __construct($host = '', $user = '', $pass,  $debug = false)
     {
     if($debug)
          $this->enableDebug();
     }

     public $debugger = false, $msg = '';
     private function debug($txt)
     {
           if(this->debugger)
                $this->msg .= '<b>Debug</b> (' . date("D h:i:s",time()) . '): ' . $txt . '<br>';
     }

     public function enableDebug()
     {
          $this->debugger = true;
          $this->debug('Starting Debugger');
     }
}

$mysql = new mysql('127.0.0.1', 'root', 'root', true);


Now let's 'run' the debugger

<?php
class mysql
{
     function __construct($host = '', $user = '', $pass,  $debug = false)
     {
     if($debug)
          $this->enableDebug();
     }

     public $debugger = false, $msg = '';
     private function debug($txt)
     {
           if(this->debugger)
                $this->msg .= '<b>Debug</b> (' . date("D h:i:s",time()) . '): ' . $txt . '<br>';
     }

     public function enableDebug()
     {
          $this->debugger = true;
          $this->debug('Starting Debugger');
     }
}

$mysql = new mysql('127.0.0.1', 'root', 'root', true);

echo $mysql->msg;


Great, so now that that is done, we need to actually connect to the server, and do all that fun stuff. We're going to add 5 variables, the 3 server information variables and two variables to store wether or not the script is connected to a server/database.

<?php
class mysql
{
     private $username, $password, $host;
     public $connected, $dbselected;

     function __construct($host = '', $user = '', $pass,  $debug = false)
     {
     if($debug)
          $this->enableDebug();
     }

     public $debugger = false, $msg = '';
     private function debug($txt)
     {
           if(this->debugger)
                $this->msg .= '<b>Debug</b> (' . date("D h:i:s",time()) . '): ' . $txt . '<br>';
     }

     public function enableDebug()
     {
          $this->debugger = true;
          $this->debug('Starting Debugger');
     }
}

$mysql = new mysql('127.0.0.1', 'root', 'root', true);

echo $mysql->msg;


Now, first thing we want to do is store all those server information variables into the class. So, let's make a new function:

<?php
class mysql
{
     private $username, $password, $host;
     public $connected, $dbselected;

     function __construct($host = '', $user = '', $pass,  $debug = false)
     {
     if($debug)
          $this->enableDebug();
     $this->setCon($host, $user, $pass);
     }
 
     private function setCon($host = '', $user = '', $pass = '')
     {
		$this->host = $host;
		$this->username = $user;
		$this->password = $pass;     
     }

     public $debugger = false, $msg = '';
     private function debug($txt)
     {
           if(this->debugger)
                $this->msg .= '<b>Debug</b> (' . date("D h:i:s",time()) . '): ' . $txt . '<br>';
     }

     public function enableDebug()
     {
          $this->debugger = true;
          $this->debug('Starting Debugger');
     }
}

$mysql = new mysql('127.0.0.1', 'root', 'root', true);

echo $mysql->msg;


Now let's connect to our server, using another function :)

<?php
class mysql
{
     private $username, $password, $host;
     public $connected, $dbselected;

     function __construct($host = '', $user = '', $pass,  $debug = false)
     {
     if($debug)
          $this->enableDebug();
     $this->setCon($host, $user, $pass);
     $this->Con();
     }
 
     private function setCon($host = '', $user = '', $pass = '')
     {
		$this->host = $host;
		$this->username = $user;
		$this->password = $pass;     
     }

     private function Con()
     {
		$this->debug('Attempting to Connect');
		$con = @mysql_connect($this->host, $this->username, $this->password);
		if($con)
		{
			$this->debug('Connected to server');
			$this->connected = true;
			return true;
		}
		else
		{
			$this->debug('Could not connect');
			$this->connected = false;
			return false;
		}
     }

     public $debugger = false, $msg = '';
     private function debug($txt)
     {
           if(this->debugger)
                $this->msg .= '<b>Debug</b> (' . date("D h:i:s",time()) . '): ' . $txt . '<br>';
     }

     public function enableDebug()
     {
          $this->debugger = true;
          $this->debug('Starting Debugger');
     }
}

$mysql = new mysql('127.0.0.1', 'root', 'root', true);

echo $mysql->msg;


Okay, now let's add in the database connect part.

<?php
class mysql
{
     private $username, $password, $host;
     public $connected, $dbselected;

     function __construct($host = '', $user = '', $pass,  $debug = false)
     {
     if($debug)
          $this->enableDebug();
     $this->setCon($host, $user, $pass);
     $this->Con();
     }
 
     private function setCon($host = '', $user = '', $pass = '')
     {
		$this->host = $host;
		$this->username = $user;
		$this->password = $pass;     
     }

     private function Con()
     {
		$this->debug('Attempting to Connect');
		$con = @mysql_connect($this->host, $this->username, $this->password);
		if($con)
		{
			$this->debug('Connected to server');
			$this->connected = true;
			return true;
		}
		else
		{
			$this->debug('Could not connect');
			$this->connected = false;
			return false;
		}
     }

     public function SetDB($db = '')
     {
		if($this->connected)
		{
			$db = @mysql_select_db($db);
			if($db)
			{
				$this->dbselected = true;
				$this->debug('Successful database connection');
				return true;
			}
			if(!$db)
			{
				$this->dbselected = false;
				$this->debug('Could not select database');
				return false;
			}
		}
		else
		{
			$this->debug('Could not select database, no server connection!');
		}
     }

     public $debugger = false, $msg = '';
     private function debug($txt)
     {
           if(this->debugger)
                $this->msg .= '<b>Debug</b> (' . date("D h:i:s",time()) . '): ' . $txt . '<br>';
     }

     public function enableDebug()
     {
          $this->debugger = true;
          $this->debug('Starting Debugger');
     }
}

$mysql = new mysql('127.0.0.1', 'root', 'root', true);
$mysql->SetDB('world');

echo $mysql->msg;


And that's it! :)

edit: I worked on it some more, and added queries, result count, automatic echo of the debug if the debug is true, a mini config spot at the top for the settings, and more debug messages. Here's the script (this one is tested!)

<?php

$host = '127.0.0.1';
$user = 'root';
$pass = 'root';
$debug = true;

class mysql
{
	private $username, $password, $host;
	public $connected, $dbselected;
	
	function __construct($host = '', $user = '', $pass = '', $debug = false)
	{
		if($debug)
			$this->enableDebug();
			
		$this->debug('MySQL class loaded');
		$this->setCon($host, $user, $pass);
		$this->Con();
		
		if($debug)
			echo '<h1>Debug</h1><br>' . $this->debug . '<br>';
	}
	
	private function setCon($host = '', $user = '', $pass = '')
	{
		$this->host = $host;
		$this->username = $user;
		$this->password = $pass;
	}
	
	public function testCon()
	{
		$this->debug('Testing Connection');
		$test = @mysql_connect($this->host, $this->username, $this->password);
		if($test)
		{
			$this->debug('Successfully connected to server');
			return true;
		}
		else
		{
			$this->debug(mysql_error());
			return false;
		}

		$test = null;
	}
	
	private function Con()
	{
		$this->debug('Attempting to Connect');
		$con = @mysql_connect($this->host, $this->username, $this->password);
		if($con)
		{
			$this->debug('Connected to server');
			$this->connected = true;
			return true;
		}
		else
		{
			$this->debug('Could not connect');
			$this->connected = false;
			return false;
		}
	}
	
	public function SetDB($db = '')
	{
		if($this->connected)
		{
			$db = @mysql_select_db($db);
			if($db)
			{
				$this->dbselected = true;
				$this->debug('Successful database connection');
				return true;
			}
			if(!$db)
			{
				$this->dbselected = false;
				$this->debug('Could not select database');
				return false;
			}
		}
		else
		{
			$this->debug('Could not select database, no server connection!');
		}
	}
	
	private $query = false;
	public function Query($query = '')
	{
		if($this->connected && $this->dbselected)
		{
			$query = @mysql_query($query);
			if($query)
			{
				$this->debug('Query executed successfully');
				$this->query = $query;
				return $query;
			}
			else
			{
				$this->debug('Error in query');
				$this->query = false;
				return false;
			}
		}
		else
		{
			$this->debug('Connection not established properly');
			return false;
		}
	}
	
	public function ResultCount()
	{
		if($this->query)
		{
			return mysql_num_rows($this->query);
		}
		else
		{
			$this->debug('Result Count failed, invalid query');
			return false;
		}
	}
	
	public $debugger = false, $debug = '';
	private function debug($txt = '')
	{
		if($this->debugger)
			$this->debug .= '<b>Debug</b> (' . date("D h:i:s",time()) . '): ' . $txt . '<br>';
	}
	
	public function enableDebug()
	{
		$this->debugger = true;
		$this->debug('Starting Debugger');
	}

}

$mysql = new mysql($host, $user, $pass, $debug);

//This is just an example, to show you how it works
$mysql->SetDB('world');
$query = $mysql->Query('SELECT * from ai_agents');
echo $mysql->ResultCount();

?>

1

#2 User is offline   Bob Herman 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 452
  • Joined: 11-October 08
  • Gender:Male

Posted 18 September 2009 - 06:41 PM

I strongly recommend you read my previous tutorials. If you don't you might have a hard time understanding this.

THIS IS ADVANCED PHP. (Just making sure you know :D )

I will finish the tut later. I still have to make the connection script (I don't recommend doing OOP off the top of your head- which was how I did the other tuts) which might take some time. And anyways, GTA finished downloading! :P
0

#3 User is offline   Bob Herman 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 452
  • Joined: 11-October 08
  • Gender:Male

Posted 20 November 2009 - 06:50 PM

Updated. Not quite done though (you might tell that it's not very well explained)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users