Object Oriented Concepts In PHP5 (Part 1)

1. objects
The main difference in OOP as opposed to functional programming is that the
data and code are bundled together into one entity, which is known as an object. Object-oriented applications are usually split up into a number of objects that interact with each other. Each object is usually an entity of the problem, which is self-contained and has a bunch of properties and methods.The properties are the object’s data, which basically means the variables that
belong to the object. The methods — if you are coming from a functional background — are basically the functions that the object supports. Going one step further, the functionality that is intended for other objects to be accessed and used during interaction is called an object’s interface.
Figure (A) represents a class. A class is a template for an object and describes what methods and properties an object of this type will have. In this example, the class represents a person. For each person in your application, you can make a separate instance of this class that represents the person’s information. For example, if two people in our application are called Joe and Judy, we would create two separate instances of this class and would call the setName() method of each with their names to initialize the variable holding the person’s name,$name. The methods and members that other interacting objects may use are a class’s contract. In this example, the person’s contracts to the outside world are the two set and get methods,setName()and get- Name().


class Person
Methods:
setName($name)
getName()

Properties:
$name
Fig.(A) Diagram of class Person.
The following PHP code defines the class, creates two instances of it, sets
the name of each instance appropriately, and prints the names:
class Person {
private $name;
function setName($name)
{
$this->name = $name;
}
function getName()
{
return $this->name;
}
};
$judy = new Person();
$judy->setName("Judy");
$joe = new Person();
$joe->setName("Joe");
print $judy->getName() . "\n";
print $joe->getName(). "\n";


2. DECLARING A CLASS
You might have noticed from the previous example that declaring a class (an object template) is simple. You use the class keyword, give the class a name,and list all the methods and properties an instance of this class should have:
class MyClass {
... // List of methods
...
... // List of properties
...
}
You may have noticed that, in front of the declaration of the $name property,we used the private keyword. We explain this keyword in detail later, but it basically means that only methods in this class can access $name .It forces anyone wanting to get/set this property to use the getName() and setName()methods, which represent the class’s interface for use by other objects or source code.


3. THE new KEYWORD AND CONSTRUCTORS
Instances of classes are created using the new keyword. In the previous example,
we created a new instance of the Person class using $judy = new Person();.
What happens during the new call is that a new object is allocated with its own copies of the properties defined in the class you requested, and then the constructor of the object is called in case one was defined. The constructor is a method named __construct(), which is automatically called by the new keyword after creating the object. It is usually used to automatically perform various initializations such as property initializations. Constructors can also accept arguments, in which case, when the new statement is written, you also need to send the constructor the function parameters in between the parentheses.
In PHP 4, instead of using __construct() as the constructor’s name, you had to define a method with the classes’ names, like C++. This still works with PHP 5, but you should use the new unified constructor naming convention for new applications.
We could have rewritten the previous example to pass the names of the people on the
new line:
class Person {
function __construct($name)
{
$this->name = $name;
}
function getName()
{
return $this->name;
}
private $name;
};
$judy = new Person("Judy") . "\n";
$joe = new Person("Joe") . "\n";
print $judy->getName();
print $joe->getName();
This code has the same result as the previous example.
NOTE : Because a constructor cannot return a value, the most common practice
for raising an error from within the constructor is by throwing an exception.
Source : PHP5 Power Programming