PHP Main: It's like Christmas come early.
PHP 5 is best for OO.
This is NOT a beginner's tutorial, I expect you should have a strong working knowledge of PHP. I'd say intermediate as I'm only touching on basic OOP functionality. Put all the class code into a file called 'classes.php' and the rest can go in another php file in the same directory.
What is OOP and why should I care?
Ironically this was me a few months back. I was generally confused by OOP, what it was, why the code looked huge, etc. But I gave it a try, and now I'm loving it. It's very much part of my workflow.
So what is it? OOP basically is object orientated, that is to say, you can have multiple instances of the same code in 'objects'. Why is this useful? Well, it makes code extremely reusable.
For example, the app I'm writing has a file with three lines of code in it (I don't count the <?php ?> tags):
<?php
require('classes.php');
$init = new Init();
$init->runApp();
?>
What does this mean? Well, here we're hooking into a class called Init, then calling a function after it, called runApp();
The basic OOP structure for this would be: (note, do NOT copy and paste this, the hyphens are there for a tab replacement)
class Init
{
---function runApp()
---{
------echo "Application successfully ran at " . date("H:m:s");
---}
}
So, there we have a base class. We can obviously add functions to it, and arguments work as normal. Nothing particularly special as of yet, I mean if you removed the class around that code you'd have a basic PHP function.
What's good about OOP is we can have a child class of our initial class, which will inherit/extend our base class. For instance:
class Action extends Init
{
---var $view;
/* In OO it is good practice to define variables that you will use */
---function runApp()
---{
------echo "App is running fine!";
---}
---function viewWin($view)
---{
------echo "Viewing... $view";
---}
}
PHP code:
<?php
require('classes.php');
$action = new Action();
$action->runApp();
$action->viewWin('Programming Forum');
?>
You can imagine this could be useful for an rpg - have the base player class, then define extensions for different species/abilities. PHP will overwrite the base class functions with the function defined in the extension - but if you don't want to overwrite then just don't define the function in the extension.
OO is useful in that functions of a class can call each other, by using the $this-> prefix. For example:
class Operation
{
---var $clean;
---function getWindow()
---{
------$clean = $this->isClean();
------if ($clean == true)
------{
---------echo "Wade hands you a ten dollar bill. 'Good job, friend!'";
------}
------else
------{
---------echo "Wade punches you in the face. 'Get cleaning that window!'";
------}
---}
---function isClean()
---{
------if ($_POST['input'] == "I cleaned the window!")
------{
---------return true;
------}
------else
------{
---------return false;
------}
---}
}
<?php
$op = new Operation();
if (!empty($_POST['input'])) { $op->getWindow(); }
?>
Whew! So this will check whether you've cleaned Wade's window properly (you BETTER have) and attack you if you haven't. :-)
The nice thing here is that I could adapt this script for multiple users - add Tom in to the mix for example, and do the isClean(); function - but have a different getWindow function that would output something else.
OO is also good for turning routine copy/pasting or including into functions you can use more often. For example, you could easily make a database class and use that instead of doing the usual mysql_connect, mysql_query etc.
I suggest you get stuck in and read the more advanced topics in the PHP manual. OO is a much neater, cleaner, more reusable method of coding applications - go for it!
----------
No rabbits were harmed during the making of this. No code has been tested so this is all off the top of my head... :-)