Oop PHP
Oop PHP
Similarly we
can imagine our car made of different objects like wheel, steering, gear etc. Same way there is
object oriented programming concepts which assume everything as an object and implement a
software using different objects.
Before we go in detail, lets define important terms related to Object Oriented Programming.
● Class − This is a programmer-defined data type, which includes local functions as well
as local data. You can think of a class as a template for making many instances of the
same kind (or class) of object.
● Object − An individual instance of the data structure defined by a class. You define a
class once and then make many objects that belong to it. Objects are also known as
instance.
● Member Variable − These are the variables defined inside a class. This data will be
invisible to the outside of the class and can be accessed via member functions. These
variables are called attribute of the object once an object is created.
● Member function − These are the function defined inside a class and are used to
access object data.
● Inheritance − When a class is defined by inheriting existing function of a parent class
then it is called inheritance. Here child class will inherit all or few member functions
and variables of a parent class.
● Parent class − A class that is inherited from by another class. This is also called a base
class or super class.
● Child Class − A class that inherits from another class. This is also called a subclass or
derived class.
● Polymorphism − This is an object-oriented concept where same function can be used
for different purposes. For example, function name will remain same but it take
different number of arguments and can do different task.
● Overloading − a type of polymorphism in which some or all of operators have different
implementations depending on the types of their arguments. Similarly functions can
also be overloaded with different implementation.
● Data Abstraction − Any representation of data in which the implementation details are
hidden (abstracted).
● Encapsulation − refers to a concept where we encapsulate all the data and member
functions together to form an object.
● Constructor − refers to a special type of function which will be called automatically
whenever there is an object formation from a class.
● Destructor − refers to a special type of function which will be called automatically
whenever an object is deleted or goes out of scope.
● Defining PHP Classes
<?php
class phpClass {
var $var1;
var $var2 = "constant string";
Example
Here is an example which defines a class of Books type −
<?php
class Books {
/* Member variables */
var $price;
var $title;
/* Member functions */
function setPrice($par){
$this->price = $par;
}
function getPrice(){
echo $this->price ."<br/>";
}
function setTitle($par){
$this->title = $par;
}
function getTitle(){
echo $this->title ." <br/>";
}
}
?>
The variable $this is a special variable and it refers to the same object ie. itself.
Creating Objects in PHP
Once you defined your class, then you can create as many objects as you like of that class type.
Following is an example of how to create object using new operator.
$physics = new Books;
$maths = new Books;
$chemistry = new Books;
Here we have created three objects and these objects are independent of each other and they
will have their existence separately. Next we will see how to access member function and
process member variables.
Calling Member Functions
After creating your objects, you will be able to call member functions related to that object.
One member function will be able to process member variable of related object only.
Following example shows how to set title and prices for the three books by calling member
functions.
$physics->setTitle( "Physics for High School" );
$chemistry->setTitle( "Advanced Chemistry" );
$maths->setTitle( "Algebra" );
$physics->setPrice( 10 );
$chemistry->setPrice( 15 );
$maths->setPrice( 7 );
Now you call another member functions to get the values set by in above example −
$physics->getTitle();
$chemistry->getTitle();
$maths->getTitle();
$physics->getPrice();
$chemistry->getPrice();
$maths->getPrice();
This will produce the following result −
Physics for High School
Advanced Chemistry
Algebra
10
15
7
Constructor Functions
Constructor Functions are special type of functions which are called automatically whenever an
object is created. So we take full advantage of this behavior,by initializing many things through
constructor functions.
PHP provides a special function called __construct() to define a constructor. You can pass as
many as arguments you like into the constructor function.
Following example will create one constructor for Books class and it will initialize price and title
for the book at the time of object creation.
function __construct( $par1, $par2 ) {
$this->title = $par1;
$this->price = $par2;
}
Now we don't need to call set function separately to set price and title. We can initialize these
two member variables at the time of object creation only.
$physics = new Books( "Physics for High School", 10 );
$maths = new Books ( "Advanced Chemistry", 15 );
$chemistry = new Books ("Algebra", 7 );
$physics->getPrice();
$chemistry->getPrice();
$maths->getPrice();
This will produce the following result −
Physics for High School
Advanced Chemistry
Algebra
10
15
7
Destructor
Like a constructor function you can define a destructor function using function __destruct().
You can release all the resources with-in a destructor.
Inheritance
PHP class definitions can optionally inherit from a parent class definition by using the extends
clause. The syntax is as follows −
class Child extends Parent {
<definition body>
}
The effect of inheritance is that the child class (or subclass or derived class) has the following
characteristics −
● Automatically has all the member variable declarations of the parent class.
● Automatically has all the same member functions as the parent, which (by default) will
work the same way as those functions do in the parent.
Following example inherit Books class and adds more functionality based on the requirement.
class Novel extends Books {
var $publisher;
function setPublisher($par){
$this->publisher = $par;
}
function getPublisher(){
echo $this->publisher. "<br />";
}
}
Now apart from inherited functions, class Novel keeps two additional member functions.
Function Overriding
Function definitions in child classes override definitions with the same name in parent classes.
In a child class, we can modify the definition of a function inherited from parent class.
In the following example getPrice and getTitle functions are overridden to return some values.
function getPrice() {
echo $this->price . "<br/>";
return $this->price;
}
function getTitle(){
echo $this->title . "<br/>";
return $this->title;
}
Public Members
Unless you specify otherwise, properties and methods of a class are public. That is to say, they
may be accessed in three possible situations −
● From outside the class in which it is declared
● From within the class in which it is declared
● From within another class that implements the class in which it is declared
Till now we have seen all members as public members. If you wish to limit the accessibility of
the members of a class then you define class members as private or protected.
Private members
By designating a member private, you limit its accessibility to the class in which it is declared.
The private member cannot be referred to from classes that inherit the class in which it is
declared and cannot be accessed from outside the class.
A class member can be made private by using private keyword infront of the member.
class MyClass {
private $car = "skoda";
$driver = "SRK";
function __construct($par) {
// Statements here run every time
// an instance of the class
// is created.
}
function myPublicFunction() {
return("I'm visible!");
}
function __construct($par) {
// Statements here run every time
// an instance of the class
// is created.
}
function myPublicFunction() {
return("I'm visible!");
}
Ex:1
<?php
class a
{
function fun1()
{
echo "BMU";
}
}
class b extends a
{
function fun2()
{
echo "SSSIT";
}
}
$obj= new b();
$obj->fun1();
?>
Ex:-2
<?php
class demo
{
public function display()
{
echo "example of inheritance “;
}
}
class demo1 extends demo
{
public function view()
{
echo "in php";
}
}
$obj= new demo1();
$obj->display();
$obj->view();
?>
Interface
o An interface is like a class except that it cannot contain code.
o An interface can define method names and arguments, but not the contents of the
methods.
o Any classes implementing an interface must implement all methods defined by the
interface.
o A class can implement multiple interfaces.
o An interface is declared using the "interface" keyword.
Example 1
<?php
interface a
{
public function dis1();
}
interface b
{
public function dis2();
}
class demo implements a,b
{
public function dis1()
{
echo "method 1...";
}
public function dis2()
{
echo "method2...";
}
}
$obj= new demo();
$obj->dis1();
$obj->dis2();
?>
Ex:-2
<?php
interface i1
{
public function fun1();
}
interface i2
{
public function fun2();
}
class cls1 implements i1,i2
{
function fun1()
{
echo "javatpoint";
}
function fun2()
{
echo "SSSIT";
}
}
$obj= new cls1();
$obj->fun1();
$obj->fun2();
?>
Introspection
https://www.youtube.com/watch?v=HBi_wWcR1W8