This week I'm covering Object Oriented Programming. Note that this is an entirely seperate "type" altogether (one I may add that I love). We havent really done any large applications/scripts - but with OOP you'll start to see how useful it can be when we start on our main project.
Up until now we've been using Integers, or strings; essentially single data types ( or basic data types). We can create our own data types by using Classes. These are sort of like compound types...
class Car {
$speed;
$direction; // 0-360 degrees
function increaseSpeed() { $this->speed += 10; }
function turnRight() { $this->direction += 90; }
}This gives is a good starting point for classes. Firstly, we create a new "Instance" of this class/object by using the new operator:
$myHonda = new Car(); so now I can go: $myHonda->speed = 40; $myHonda->turnRight(); echo "My Speed: ".$myHonda->speed."\n";
As you can see, my compound data type called "Car" makes it much easier to "organise" my code. So if I wanted 2 cars, I could simply create two instances of the object.
So, cant I acheive all of the above using an array of data? Sure:
$car = Array("speed"=>40, "direction"=>90);
$car['speed'] += 60; // Accelerate 60 km/hhowever - its not as nice... Try finding a nice and easy way to create a new car... without having to know all about its structure. After all - we dont build a car (like the array example) we buy them (ie, premade as in the class example).
So, what about if we tried to build a BankAccount? Well firstly, we don't want anyone to be able to modify our account balance, or password do we. So we make those member variables private, and only allow people to use the functions:
class BankAccount {
private $balance;
private $password;
public function __construct($pass) {
$this->password=$pass;
$this->balance = 10.00;
}
public function addFunds($howMuch) {
if($howMuch > 0) {
$this->balance += $howMuch;
}
}
public function checkPassword($pass) {
if($this->password == $pass) {
return True;
}
else {
return False;
}
}
}Ok. So the above examples dont cover all the needed functions.... clearly we aren't able to reset a password, or withdraw from the account... but you can see that by protecting our "Member variables", we can use functions to ensure that we can control what's happening to our function. By using the keyword private or public we are declaring the scope in which they are available in. Public means that everyone can access it. Protected means that only itself and child classes can access it (see inheritence below), and private means that only the current class can access it.
So far we've had fairly simple classes that only use basic data types. But we can have classes who's member variables are other objects. We are ( to a certain extent) extending the data you can use in PHP. For example (this will be homework), we can build a Bank class, which has an array of Employee's, and an array of BankAccounts (similar to what we built above).
Some real applications of classes and OOP is building a website or system that can easily be extended ( for example Joomla!, a PHP based CMS which easily allows you to add templates, different components (create and modify content) and even language packs. You create a basic class design ( a list of functions that the class must use) and then it doesnt matter what the class is, you create a new instance of it and call the functions. Here is a rough idea ( this isnt full code, I havent defined all of the different classes).
$pluginName = "ForumClass"; $pageClass = new $PluginName(); $pageClass->parseData($db); $pageClass->displayOnPage();
Here, we are creating a new "ForumClass" even though we dont know that. in this way, we can send the class/plugin name through a form etc.
Ok, so how does someone know how to create a plugin for your system, and how do you enforce your functions on their plugin? Interfaces:
interface Extendable {
public function parseData(mysqli $&database); // Force it to accept a reference to a mysqli object (mysql connection)
public function displayOnPage();
}
ok, so to implement this:
class myPlugin implements Extendable {
public function anotherMethod();
}When you create a new instance of the myPlugin class, it will throw out an error because it doesnt adhere to your interface rules. Note that I've also told the plugin to use a mysqli object. This is a proccess called type hinting. It tells the function what type of data
Ok, thats all I'll cover this week. I'm NOT covering inheritence and polymorphism and overloading in this tutorial. These three are essentially the "strength" behind classes - and I think you need a full week to understand them. As always, any questions etc just email or post a reply.
Thanks!
This post has been edited by Blambu: 28 June 2009 - 12:10 PM
Reason for edit: Wrapped in Code Tags

Sign In
Register
Help


MultiQuote