Object Oriented Programming in PHP

Object-oriented programming approach is very different from the traditional programming techniques where a chunk of data is sent to function to function. Objects model the real – world things, processes, and ideas that your application is designed to handle. An object-oriented application is a set of collaborating objects that independently handle certain activities.

The concepts of classes and objects and the ways in which you can use them are the fundamental ideas behind object-oriented programming. As you ‘ll see, an object-oriented approach gives you some big benefits over procedural programming.

Advantages of OOP in PHP :

  1. Code Reusability
  2. Mapping ideas into code modules
  3. Modularity

Basic Concepts :

Classes: A class is a unit of code that describes the characteristics and behaviors of something, or of
a group of things.

Objects: An object is a specific instance of a class.  The distinction between classes and objects is often confusing to those new to OOP. It helps to think of classes as something you create as you design your application, whereas objects are created and used when the application is actually run.

Methods: The behaviors of a class — that is, the actions associated with the class are known as its methods. Methods are very similar to functions; in fact, you define methods in PHP using the function statement.


Creating Classes and Objects in PHP

To create a class, you use PHP ’ s class keyword. Here ’ s a really simple class:

<?php

class Car {
// No definition
}

?>

This code simply defines a class called Car.

To create an object, you use
the new keyword, followed by the name of the class that you want to base the object on. You can then
assign this object to a variable, much like any other value.
Here ’ s an example that shows how to create objects:

<?php

class Car {
// No definition
}

$beetle = new Car();
$mustang = new Car();

?>

Declaring Properties

To add a property to a class, first write the keyword public, private, or protected — depending on
the visibility level you want to give to the property — followed by the property ’ s name (preceded by a $ symbol):

<?php

class MyClass {
public $property1; // This is a public property
private $property2; // This is a private property
protected $property3; // This is a protected property
}

?>

By the way, you can also initialize properties at the time that you declare them, much like you can with variables:

<?php
class MyClass {
public $widgetsSold = 123;
}

?>

In this case, whenever a new object is created from MyClass , the object ’ s $widgetsSold property
defaults to the value 123.


Accessing Properties

Once you have created a class property, you can access the corresponding object ’ s property value from within your calling code by using the following syntax:

$object-> property;

That is, you write the name of the variable storing the object, followed by an arrow symbol composed of a hyphen ( – ) and a greater – than symbol (>), followed by the property name. (Note that the property name doesn’t ’ t have a $ symbol before it.)

< ?php

class Car {
 public $color;
 public $manufacturer;
}

$beetle = new Car();
$beetle- > color = “red”;
$beetle- > manufacturer = “Volkswagen”;

$mustang = new Car();
$mustang- > color = “green”;
$mustang- > manufacturer = “Ford”;

echo “ Some properties:  ”;
echo “  The Beetle’s color is “ . $beetle- > color . ”;
echo “ The Mustang’s manufacturer is “ . $mustang- > manufacturer . ”;
? >

The corresponding output is:

 Some properties:
  The Beetle’s color is red.
   The Mustang’s manufacturer is Ford.

Class Constants

You can create constants which are special identifiers that hold fixed values
throughout the running of your script. PHP also lets you create constants within classes. To define a class
constant, use the keyword const, as follows:

<?php

class MyClass {
const MYCONST = 123;
}

?>

Working with Methods

As mentioned earlier, a method is much like a function, except that it ’ s tied to a specific class.
Method Visibility. As we know that variables can have three visibility levels: public, private, and protected. The same is true of methods. All methods can be called by other methods within the same class. If the method is declared public, any code outside the class definition can also potentially call the method. However, if a method is declared private, only other methods within the same class can call it. Finally, a protected method can be called by other methods in the class, or in a class that inherits from the class.


Creating a Method

To add a method to a class, use the public , private , or protected keyword, then the function
keyword, followed by the method name, followed by parentheses. You then include the method ’ s code within curly braces:

<?php
class MyClass {

public function aMethod() {
// (do stuff here)
}

}

?>

Calling Methods

To call an object ’ s method, simply write the object ’ s name, then the same arrow used for accessing properties ( – > ), then the method name followed by parentheses:

$object-> method();

Note: Just like the normal functions we can have formal and default parameters for the functions tied to the classes.

Leave a Comment

Your email address will not be published. Required fields are marked *