Object Oriented Programming
OOP is a method of programming where objects are designed and communicate with each other to solve a problem.
The three major ideas of object-oriented programming (OOP) are:
1. Inheritance
2. Polymorphism
3. Encapsulation
These subjects are for later tutorials.
Objects are designed to represent real world objects. Some objects in the real world include: books, paper, and a shoe. Anything that has a unique identity can be considered an object.
Object Oriented Languages
There are several object-oriented programming languages. A few of these languages include: PHP, C++, and Java. Of all these languages Java is a full object-oriented language, where as C++ and PHP are not. They include object features but are not a fully object-oriented language.
The focus on this tutorial is object-oriented programming in the PHP language.
Defining an object
You use the class keyword to define a class. A class is a template that you can use to create several instances of an object.
Example:
A class is a method of packaging data and methods in one coherent package. The methods act on the data. This class above lets you construct several objects of type player.
Constructors
A object is created by using a constructor. In Java, a constructor is defined by writing a function that named after the type of object.
Example the constructor a player object would look like:
Constructors in PHP5 can be written in this method.
Example:
Code:
function test($a,$b) {
$this->a = $a;
$this->b = $b;
}
This is more PHP4 style constructor. There is a newer way of writing constructors.
Code:
function __construct($a,$b) {
$this->a = $a;
$this->b = $b;
}
You call the constructor by using the new operator. This creates a new object.
Example:
Code:
$test = new test(5,3);
Now $test is a reference to a test object. This is important to note that variables holding objects are actually a reference to the objects location in memory.
The $this Variable
this is a special variable that refers to the current object. Which is why in my constructor I used $this->a = $a. This means that in the current object the $a variable should be set to the local $a variable.
An example class:
Code:
class test {
public $a;
public $b;
function __construct($a,$b) {
$this->a = $a;
$this->b = $b;
}
}
If you left off $this->a and just put $a = $a. The local variable $a would have its value changed. This is a common thing to get confused about if you come from java where using this is optional. In PHP you must include the $this operator.
Instance variables
$a and $b defined at the beginning of test are instance variables of the class. Any object created of type test will have it's own $a and $b variable.
Example:
Code:
$test1 = new test(5,3);
$test2 = new test(4,2);
echo "a = {$test1->a}, b = {$test1->b}<br />";
echo "a = {$test2->a}, b = {$test2->b}<br />";
When you run this the output would be:
a = 5, b = 3
a = 4, b = 2
This example illustrates that each object is unique from each other, and that they have their own variables.
Instance Methods
Functions in a class are referred to as methods. A method is defined to act on data in the object. A class method is written in the same way as writing a function.
Code:
function getA() {
return $this->a;
}
This method called a getter just returns the value of $a. Every object created from the class would have it's own getA method. The class again with this method:
Code:
class test {
public $a;
public $b;
function __construct($a,$b) {
$this->a = $a;
$this->b = $b;
}
function getA() {
return $this->a;
}
}
Example of calling the function:
Code:
$test = new test(5,3);
echo $test->getA();
This would output just "5". The proper use of getters and setter with visibility modifiers can make your data more secure by only allowing parts of program access to the data it absolutely requires. More on access modifiers later.
Destructors
When an object is no longer used it's destructor is called. This is a special function that usually is used to handle cleanup the object and freeing up memory.
You can write a destructor like this:
Code:
function __destructor() {
echo "Bye!";
}
When the script is done executing all destructors are called to clean up memory.
Different Control Modifiers
Control modifiers allow you to control what parts of your program can access other parts of the program. This can make your program easier to maintain and provide for more security. A class that maintains information about how many visits a page has received should have absolutely no access to the customer credit card info. With proper use of access modifiers the object only cans access to variables and methods that is absolutely requires access to.
The different access modifiers are:
public
When you use the public modifier the variable or method can be accessed by any object or function. This is the default access modifier.
private
When you use the private modifier the variable or method can only be accessed by the object that it is a part of. Most instance variables should be declared private and use the appropriate setters and getter methods.
final
This variable or function is declared final. This means that you cannot override this variable in an inherited class.
Abstract
This means that you cannot initialize this class. You have to inherit from this class. The "abstract" keyword indicates an abstract class.
protected
protected means that the variable or function can only be used by the object or classes that are inheriting it.
Setters and Getters
A setter is a method that updates a variable. This is more secure than allowing direct access to a variable. Example: with a setter you can make sure that the user has enough money in their account for a withdraw.
Example:
Code:
function setBalance($amount) {
if ($amount > $this->amount) {
echo "You don't have enough money to make this withdraw.";
return false;
}
$this->amount -= $amount;
return true;
}
This setter function named with the word "set" only updates the balance in the object if the correct amount exists to make the withdrawl. In the class definition "$amount" would be declared private.
A getter method simply returns one of the objects variables.
Example:
Code:
public function getName() {
return $this->name;
}
This method if placed in a class would return the value of the objects $name variable.
Inheritance and Constructors
If you are inheriting a class and you would like to call the superclass' constructor and slightly modify it you can use the parent::__construct() function. This special function calls the superclass' constructor. You would use this is an overridden constructor.
For example:
Code:
class person extends DNA {
public $name = "";
public $age;
function __construct($name,$age) {
// call the DNA constructor to create this persons DNA structure
parent::__construct($name);
$this->age = $age;
}
}
The extends keyword allows you to create a new class based on a different class. This is known as "inheritance". The new class gains access to all public, and protected variables of the parent.
You can delete objects by using the unset method.
Copying Objects
The one great thing about the object model in PHP is it allows easy copying of objects. Say you are implementing a recursive walkabout through a maze and you need several copies of an object you can use the clone keyword to pass a copy.
Example:
Code:
function walkabout($maze) {
// at the beginning of the recursive walkabout we need a new copy of the maze object
$copy = clone $maze;
}
Here the $copy variable is a copy of the $maze object.
This is an introduction to object based programming. Using objects correctly can make your program easier to understand and a lot easier to maintain. Not to mention securing your data in a more easier way.
Enjoy,
James
Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum