0% found this document useful (0 votes)
2 views

Unit 1

This document provides an introduction to Object-Oriented Programming (OOP) in PHP, explaining key concepts such as classes, objects, inheritance, and interfaces. It highlights the advantages of OOP over procedural programming, including code reusability and easier maintenance. The document also includes examples and syntax for creating classes, using the $this keyword, and implementing interfaces in PHP.

Uploaded by

shraddhavinod1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit 1

This document provides an introduction to Object-Oriented Programming (OOP) in PHP, explaining key concepts such as classes, objects, inheritance, and interfaces. It highlights the advantages of OOP over procedural programming, including code reusability and easier maintenance. The document also includes examples and syntax for creating classes, using the $this keyword, and implementing interfaces in PHP.

Uploaded by

shraddhavinod1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Unit 1

Introduction to Object Oriented Programming in PHP

Introduction-:
Php is an Object-oriented language. OOP stands for Object-Oriented
Programming. OOP is a programming model that is based on the concept of
class and object.
Procedural programming is about writing procedures or functions that perform
operations on the data, while object-oriented programming is about creating
objects that contain both data and functions. An object oriented language is a
language where data types of data structure are defined by the programmer as
well as their properties.
Object-oriented programming has several advantages over procedural
programming:
 OOP is faster and easier to execute
 OOP provides a clear structure for the programs
 OOP helps to keep the PHP code DRY "Don't Repeat Yourself", and
makes the code easier to maintain, modify and debug
 OOP makes it possible to create full reusable applications with less code
and shorter development time.
Class objects
Fruit Apple
Banana
Mango
---------------------------------------------------------------------------------
Class objects
Car Maruti
Audi
BMW
The object oriented concept in php are:-
1) Class 2) Object 3) Inheritance 4) Interface
5) Abstraction
1) Class-: class is a programmer defined data type, which includes local
methods(function) and local variable. Class is a collection of objects
and object has properties and behaviour. we have a class named Fruit.
A Fruit can have properties like name, color, weight, etc. We can define
variables like $name, $color, and $weight to hold the values of these
properties.
When the individual objects (apple, banana, orange etc.) are created,
they inherit all the properties and behaviors from the class, but each
object will have different values for the properties.
Creating a class-:
To create a class use the class keyword, followed by the class name,
followed by an opening bracket and closing bracket and all the code of
the class go between the brackets.

Syntax to Create Class in PHP


<?php
class classname
{
// Class properties and methods go here
}
?>
1)Example:- (class and object)
<html>
<head>
<title>Rectangle Class</title>
</head>
<body>
<?php
class Rectangle
{
public $length = 10;
public $width = 5;
public function getPerimeter()
{
return (2 * ($this->length + $this->width));
}
public function getArea()
{
return $this->length * $this->width;
}
}
// Creating an object of the Rectangle class
$obj = new Rectangle();
// Outputting the results
echo "Perimeter :" . $obj->getPerimeter() ."<br>";
echo "Area : " . $obj->getArea();
?>
</body>
</html>
----------------
//PHP - The $this Keyword
The $this keyword refers to the current object, and is only available inside
methods.
2) Example-:
<?php
class employee
{
public $name;
public $age;
public $salary;
function work()
{
echo "i am working";
}
}
$obj1=new employee();
echo $obj1->work();
?>
--------------------------------------------------------

3) Example:-
<?php
class employee
{
public $name;
public $age;
public $salary;
}
$obj1=new employee();
$obj1->name='Priya';
$obj1->age=20;
$obj1->salary=10000;

$obj2=new employee();
$obj2->name='Sakshi';
$obj2->age=18;
$obj2->salary=180000;

print_r($obj1);
echo '<br/>';
print_r($obj2);
?>
-------------------------------------------------------------------------------------------------------------------------

4) Example
<body>
<?php
class Fruit
{
// Properties
public $name;
public $color;
// Methods
function set_name($name)
{
$this->name = $name;
}
function get_name()
{
return $this->name;
}
}
$apple = new Fruit();
$banana = new Fruit();
$apple->set_name('Apple');
$banana->set_name('Banana');

echo $apple->get_name();
echo "<br>";
echo $banana->get_name();
?>
</body>
</html>
Introspection-:
Introspection is a common feature in any programming language which allows
Object classes to be manipulated by programmer. You will find introspection
particularly useful when you don’t know which class or method you need to
execute at design time. Introspection in php offers the useful ability to examine
classes, interfaces properties and methods.
Introspection is the ability of a program to examine an object's characteristics,
such as its name, parent class (if any), properties, and methods. With
introspection, you can write code that operates on any class or object. You don’t
need to know which methods or properties are defined when you write your
code; instead, you can discover that information at runtime, which makes it
possible for you to write generic debuggers, serializers, profilers, etc.
Php introspection function -:
We can use those functions to extract basic
information about classes such as their name, the name of their parent class and
so on.
 class_exists(): Checks if a class has been defined
 get_class(): Returns the name of a class
 get_parent_class(): Returns the name of a class' parent class
 is_subclass_of(): Checks if a class is a subclass of another class
 get_class_methods(): Returns the names of a class' methods
 get_class_vars(): Returns the default properties of a class
 method_exists(): Checks if an object defines a method

Example-:
class emp
{
public $eno;
public $ename;
public $esalary;
public function getEmp()
{
// code
}
public function disEmp()
{
// code
}
class manager extends emp
{
public $hra;
public $ta;

public function getMgr()


{
//code
}
public function disMgr()
{
//code
}
}
$obj=new emp();
?>

Inheritance-:
Inheritance is a fundamental object-oriented programming concept in PHP where a
class (subclass or child class) can inherit properties and methods from another class
(superclass or parent class). It enables code reusability and promotes hierarchical
relationships between classes.
It is a concept of accessing the features of one class from another class. If we inherit
the class features into another class, we can access both class properties. We can
extends the features of a class by using 'extends' keyword.
where A is the base class (also called parent called) and B is called a subclass or child
class.
Syntax for Inheriting a Class in PHP
In PHP, the syntax for inheriting a class is as follows:
class ChildClass extends ParentClass
{
// Class definition
}
In the above syntax:
 ChildClass is the name of the child class that is going to inherit from
the ParentClass.
 ParentClass is the name of the parent class that contains the properties and
methods to be inherited.
 By using the extends keyword, the child class inherits all the public and
protected properties and methods from the parent class. The child class can
then access and utilize these inherited members.
Access Modifiers in PHP
In PHP, access modifiers play an important role in inheritance, as they define the
visibility and accessibility of properties and methods in the class hierarchy. There are
three access modifiers available in PHP:
1. Public:
The public access modifier allows properties and methods to be accessed from
anywhere, including outside the class, subclasses, and instances of the class.
Public members are inherited by child classes and can be overridden or
extended.
2. Protected:
The protected access modifier limits the visibility of properties and methods to
the class itself and its subclasses. Protected members can be accessed within
the class and its subclasses, but not from outside the class hierarchy. Protected
members are inherited by child classes and can be overridden or extended.
3. Private:
The private access modifier restricts the visibility of properties and methods to
the class where they are defined. Private members cannot be accessed or
inherited by child classes or instances of the class. They are only accessible
within the class itself. Therefore, private members cannot be overridden or
extended in child classes.

Example-:
<?php
class a
{
public function f1()
{
echo "PHP";
}
}
class b extends a
{
public function f2()
{
echo "Advance Php";
}
}
$obj=new b();
$obj->f1();
echo "<br/>";
$obj->f2();
?>
OUTPUT
PHP
Advance Php
1) Example:-

<?php
class demo
{
public function display()
{
echo "Single Inheritance";
}
}

class demo1 extends demo


{
public function view()
{
echo "Multiple Inheritance";
}
}
$obj=new demo1;
$obj->display();
echo "<br/>";
$obj->view();

?>
OUTPUT
Single Inheritance
Multiple Inheritance
-----------------------------------------------------------------------------------------
2) Example-:

<?php
class myclass {
public function hello() {
echo "Hello from the parent class";
}
public function thanks() {
echo "Thank you from parent class";
}
}
class newclass extends myclass {
public function thanks() {
echo "Thank you from the child class" ;
}
}

# object of parent class


$obj1 = new myclass;
$obj1->hello();
echo '<br>';
$obj1->thanks();
echo '<br>';

# object of child class


$obj2 = new newclass;
$obj2->hello();
echo '<br>';
$obj2->thanks();
?>
OUTPUT
Hello from the parent class
Thank you from parent class
Hello from the parent class
Thank you from the child class
2) Multiple inheritance-:
Multiple Inheritance is the property of the Object Oriented Programming
languages in which child class or sub class can inherit the properties of the
multiple parent classes or super classes.

PHP doesn’t support multiple inheritance but by using Interfaces in PHP we


can implement it.
--------------------------------------------------------------------------------------------
3) Multilevel Inheritance-
Multilevel inheritance is the third type of inheritance
supported in PHP. It occurs at different levels. Here, one base class is
inherited by a derived class, then that derived class is inherited by other
derived classes, and so on.

Multilevel Inheritance is another type of inheritance that is found in PHP.


Multilevel inheritance can also be explained by a family tree. There is one
base class. This base class can be inherited by. These subclasses (not every
subclass necessarily) act as base class and are further inherited by other
subclasses. This is just like a family having descendants over generations.
Example 1-
<?php
class grandparent
{
function display1()
{
echo "Grand-Parent";
}
}
class parents extends grandparent
{
function display2()
{
echo "Parents";
}
}
class child extends parents
{
function display3()
{
echo "Child";
}
}
$obj=new child();
$obj->display1();
$obj->display2();
$obj->display3();
?>

---------------------------------------------------------------------------

Example 2
<?php
class A
{
public function myAge()
{
return "My age is 75";
}
}

class B extends A
{
public function mySonAge()
{
return "My son age is 45";
}
}
class C extends B
{
public function myGrandsonAge()
{
return "My grand son age is 10";
}
function myHistory()
{
echo "Class A :" .$this->myAge(). "<br/>";
echo "Class B :" .$this->mySonAge(). "<br/>";
echo "Class C :" .$this->myGrandsonAge(). "<br/>";
}
}

$obj=new C();
$obj->myHistory();
?>

Interface-:
A PHP interface defines a contract which a class must fulfill. If a PHP class is a
blueprint for objects, an interface is a blueprint for classes.
In PHP, the interface blocks declare a set of functions to be defined with a class
to implement this interface. A class can extend more than one interface, thereby,
we can simulate multiple inheritances in PHP.
While implementing an interface with a PHP class, we must define all the
functions declared in that interface as it is, without changing arguments
Interfaces are declared with the interface keyword.
When we define a child class, we use the keyword "extends". In this case, the
class that must use the keyword "implements".

Syntax-:
interface interface_name
{
// methods declaration (only)
}
class class_name implements interface_name
{
// methods implements
}
Rules-:
1) Interfaces cannot have properties ,only we can declare methods.
2) All interface methods must be public.( don’t use private and protected)
3) All methods in an interface are abstract, so they cannot be implemented
in code.
4) Method will be implemented only child class.
5) Don’t use access specifier.

Example 1-
<?php
interface car
{
function engine();
function speed();
}
class BMW implements car
{
public function engine()
{
echo "BMW Engine";
}
public function speed()
{
echo "100 per km";
}
}

$obj=new BMW();
$obj->engine();
echo "<br/>";
$obj->speed();
?>
Example 2
<?php
interface fruit {
public function taste();
}

class mango implements fruit {


public function taste() {
echo "very sweet !!!";
}
}

$fru = new mango();


$fru->taste();
?>
-----------------------------------------------------------------------------------
Example 3
<?php
interface parent1
{
function add($a,$b);
}

interface parent2
{
function sub($c,$d);
}

class childClass implements parent1, parent2


{
public function add($a,$b)
{
echo "Addition is :" .$a+$b;
}
public function sub($c,$d)
{
echo "Subtraction is :" .$c-$d;
}
}
$test=new childClass();
$test->add(20,15);
echo "<br/>";
$test->sub(45,15);
?>

You might also like