Object Oriented Programming in PHP

Loading

Object Oriented Programming in PHP: Tips, Tricks, and Techniques

Object-oriented programming (OOP) allows programmers to develop applications in which small pieces of reusable code are grouped together to form an object. While this concept might sound complicated, it’s simple to understand and implement in PHP if you know the right practices and tricks. If you’re ready to start using OOP in your PHP applications, follow these tips, tricks, and techniques to ensure you’re doing it safely and effectively.

Class
The PHP Class contains variables and functions.

Objects

To access the function of a class you have to create an object and using that object you have to invoke the function.

Inheritance

Suppose a class say A have 2 methods ie x() and y().Now if you create a sub class say B with extends keyword  you do not need to define function of super class.Now if you create the object of sub class and call function x() , it invokes function of super class.

Function overloading: same function name but different signature in same class is called function.

Function Overriding: same function with same function signature i super class and sub class is called function overriding.

Function signature is the combination of function name and its total number parameter list and the data type of parameter list.

Interface: Multiple inheritance can be achieve through implementing interface.You if implements a interface then you need to define all its memmer functions.

Abstraction
An abstract class is one that cannot be instantiated, but it can contain methods. As it is a class you can extend this classand define the fuctions of that class to subclass.If you do not define all the methods of the abstract class then sub class should be define with abstract keyword.

Magic Methods.
There are four magic methods that an object can have attached to it: __construct(), destruct(), __call(), and __toString(). Each of these has a purpose. Let’s take a look at each. The __construct() method is executed when the object is created. The destruct() method executes when the object is about to be destroyed. These two are for more for destructive type operations and should only be used if you know what you're doing. The __call() method allows you to use objects as callbacks or functions; this one should be avoided unless absolutely necessary (and even then, only use if you really know what you're doing). Finally, the __toString() method defines how the string representation of an object will appear in quotes when printed out or concatenated with other strings – this one needs no introduction!

 

Sample Programming:

<?php
class Book {
/* Member variables */
var $price;
var $title;
/* Member functions */
function setPrice($x){
$this->price = $x;
}
function getPrice(){
echo $this->price ."<br/>";
}
function setName($x){
$this->title = $x;
}
function getName(){
echo $this->title ." <br/>";
}
}
$php = new Book();
$java = new Book();
$python = new Book();
$php->setName( "Programming through PHP");
$java->setName( "Programming through JAVA" );
$python->setName( "Programming through PYTHON" );
$php->setPrice( 100 );
$java->setPrice( 200 );
$python->setPrice( 300 );
$php->getName();
$java->getName();
$python->getName();
$php->getPrice();
$java->getPrice();
$python->getPrice();
?>

OutPut:

Programming through PHP
Programming through JAVA
Programming through PYTHON
100
200
300