PHP Classes and Objects

PHP is an Object Oriented Programming language. The Object Oriented Programming model is based on the concept of classes and objects. A programming language can be called object-oriented if it provides four basic capabilities to developers :

Abstraction :- It is the capability to store one object inside another object. An object should characterise a certain abstract idea or task. The object should present an interface to the programmer that provides the features or services one might expect of an object of that type.

Encapsulation :- It is the capability to store related information, whether data or methods, together in an object. An object should maintain internally the state necessary to characterise its behaviour. This data is usually hidden from other objects and accessed through the public interface.

Inheritance :- It is the capability of a class to rely upon another class (or number of classes) for some of its properties and methods. The language should provides the means for specialised objects to be created from more general objects.

Polymorphism :-  It is the capability to write one function or method that works in a variety of different ways. Different objects should be able to respond in different ways to the same action.

An object-oriented language is a language where data types of data structures are defined by the programmer as well as their properties and the things that can be done with them. Classes and objects are two main aspects of object-oriented programming. Now lets take a look at them one by one.

Class : A class is a self-contained and independent collection of variables and functions which work together to perform one or more specific tasks. A class acts as a template or blueprint from which lots of individual objects can be created. For example, a programmer can create a car class which emulates a car, and this class can contain the properties of a car like colour, model, Manufacture year etc and functions (methods) like drive, reverse, stop etc. At here by using the class car, we can create several individual objects.

Object : In simple words objects are variables containing variables. Object is an individual instance of the data structure defined by a class. Objects are also known as instance.

Creating a Class :

In php a class is created by using the class keyword, followed by the class name, along with an opening brace and a closing brace. All the class properties and methods are defined and declared between the braces. The syntax would be :
class className {
  // Code goes here
}
Example :
class myClass {

  var $var1;
  var $var2;

  function myFunc($arg1, $arg2) {
    $this->var1 = $arg1;
    $this->var2 = $arg2;
  }

}
At the above example we create two class variables var1, var2 and a class method or function myFunc() with two argument. The function is used to assign values to class variable. The values are assigned  to the variables by using $this keyword along with -> operator. The $this keyword is a special variable which points to the current object. Here is another example of a class :

Example :
class myClass {
  var $name;
  var $age;

  function setData($arg1, $arg2) {
    $this->name = $arg1;
    $this->age = $arg2;
  }

  function getName() {
    return $this->name;
  }

  function getAge() {
    return $this->age;
  }

}
At above example, there two member variables name and age, and the member function setData() sets the values of both member variables. And functions getAge() and getName() returns the age and name respectively.

Creating an Object :

In PHP the object is created by using the new operator.  After defining the class we can use the new operator to create an object of that class. for example :
 $person1 = new myClass;
The above statement create a new myClass object named $person1. We can create many object from a class.
 $person2 = new myClass;
 $person3 = new myClass;

Calling Class Functions :

After creating the object, we are able to call the class functions related to that object. To call class function (or member functions) with object, again we have to use the -> operator. The syntax would be :
 $object->FunctionName();
Example :
 $person1->setData("John", 24);
 $person1->getName();
 $person1->getAge();
Now Lets see an example :
<?php

class myClass {
  var $name;
  var $age;

  function setData($arg1, $arg2) {
    $this->name = $arg1;
    $this->age = $arg2;
  }

  function getName() {
    return $this->name;
  }

  function getAge() {
    return $this->age;
  }

}

$person1 = new myClass;
$person1->setData("John", 24);

echo "Name : " . $person1->getName() . "<br/>";
echo "Age : " . $person1->getAge() . "<br/>";

?>
Output :

Name : John
Age : 24

Another Example with more than one objects  :
<?php

class myClass {
  var $name;
  var $age;

  function setData($arg1, $arg2) {
    $this->name = $arg1;
    $this->age = $arg2;
  }

  function getName() {
    return $this->name;
  }

  function getAge() {
    return $this->age;
  }

}

$person1 = new myClass;
$person2 = new myClass;
$person3 = new myClass;

$person1->setData("John", 24);
$person2->setData("David", 50);
$person3->setData("Marry", 20);


echo "Name : " . $person1->getName() . "<br/>";
echo "Age : " . $person1->getAge() . "<br/><br/>";

echo "Name : " . $person2->getName() . "<br/>";
echo "Age : " . $person2->getAge() . "<br/><br/>";

echo "Name : " . $person3->getName() . "<br/>";
echo "Age : " . $person3->getAge() . "<br/><br/>";

?>
Output :

Name : John
Age : 24

Name : David
Age : 50

Name : Marry
Age : 20

Next Topic :