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

Object Oriented Concepts

Uploaded by

Narasimhamurthy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Object Oriented Concepts

Uploaded by

Narasimhamurthy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIT-4

CLASSES AND OBJECTS IN PHP


Object Oriented Concepts
• 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 Class:


The general form for defining a new class in PHP is as follows –
<?php
class classname
{
var $var1;
var $var2 = “constant string”;
function myfunc($arg1, $arg2)
{
Codes to be executed
}
}
?>
Here is the description of each line −
• The special form class, followed by the name of the class that you want to define.
• A set of braces enclosing any number of variable declarations and function definitions.
• Variable declarations start with the special form var, which is followed by a conventional
$ variable name; they may also have an initial assignment to a constant value.
• Function definitions look much like standalone PHP functions but are local to the class
and will be used to set and access object data.
Example:
<?php
class student
{
var $sname;
var $course;
function showName($sname, $course)
{
echo "Student name is ".$sname."<br>";
echo "Course is ".$course."<br>";
}
}
$s1 = new student;
$s1->showName("Charan","BCA");
?>

Creating Objects in PHP


Once defined class, then we can create as many objects as we like of that class type using new
operator. Example:
$s1 = new student;
Here we have created three objects and these objects are independent of each other and they will
have their existence separately.

Calling Member Functions


After creating objects, we 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.
Example:
$s1->showName("Charan","BCA");

The output of above php script is:


Student name is Charan
Course is BCA

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. We can pass as
many as arguments you like into the constructor function.
Destructor:
Like a constructor function you can define a destructor function using function __destruct(). We
can release all the resources with-in a destructor.
Example for constructor and destructor:

<?php
class Student
{
private $sname;
private $course;
public function __construct($sname, $course)
{
echo "Initializing the objects<br><br>";
$this->sname = $sname;
$this->course = $course;
}
public function __destruct()
{
echo "<br><br><br>Detroying Object";
}
public function showName()
{
echo "Student name is : ". $this->sname;
echo "<br>Course is: ".$this->course;
}
}
$s1 = new Student("Charan","BCA");
$s1->showName();
?>
The output is:
Initializing the objects
Student name is : Charan
Course is: BCA
Detroying Object

The variable $this is a special variable and it refers to the same object ie. itself.

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
{
<body definition>
}
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 a member function as the parent, which (by default) will
work the same way as those functions do in the parent.
Different types of Inheritance
1) Simple Inheritance
2) Multilevel Inheritance.
3) Hierarchical Inheritance.

Simple Inheritance:
In simple inheritance there will be exactly one super class and one subclass i.e. subclass will get
the functionality from exactly only one super class. It is supported by class data type.
Figure:

<?php
class Box
{
public $width;
public $height;
public $depth;
function __construct($width, $height, $depth)
{
$this->width = $width;
$this->height = $height;
$this->depth = $depth;
}
public function volume ()
{
return $this->width * $this->height * $this->depth;
}
}
class BoxWeight extends Box
{
public $mass;
function __construct($width, $height, $depth, $mass)
{
$this->width = $width;
$this->height = $height;
$this->depth = $depth;
$this->mass = $mass;
}
public function density ()
{
return $this->volume () * $this->mass;
}
}
$boxWeight = new BoxWeight(10, 20, 30, 40);
?>

Multilevel Inheritance:
In multilevel inheritance one super class can have many sub classes. One subclass can have
many indirect super classes i.e. one is direct super class all remaining are indirect super classes.
Figure:

<?php
class Aves
{
public function nature()
{
echo "Generally, Aves fly";
}
}
class Bird extends Aves
{
public function eat()
{
Echo "Eats to live";
}
}
class Parrot extends Bird
{
public function food()
{
echo "Parrot eats seeds and fruits";
}
}
?>
Hierarchical Inheritance:
In this one super class can have many direct sub classes. It is supported by class data type.
Figure:

<?php
class Aves
{
public function nature()
{
echo "Generally, Aves fly";
}
}
class Parrot extends Aves
{
public function eat()
{
echo "Parrot eats fruits and seeds";
}
}
class Vulture extends Aves
{
public function vision()
{
echo "Vulture can see from high altitudes";
}
}
?>
Overloading
In PHP, overloading typically refers to two concepts: method overloading and property
overloading.
Method Overloading: PHP doesn't support method overloading in the same way that language
like Java do, where we can have multiple methods with the same name but different parameters.
However, we can simulate method overloading using variable-length argument lists or optional
arguments.
Property Overloading: PHP supports property overloading through the magic methods __get()
and __set(). These methods allow you to intercept attempts to read or write inaccessible
properties and handle them dynamically.
Here’s an example of how we can implement method overloading in PHP using variable-length
argument lists or optional arguments:
class OverloadingExample
{
public function add()
{
$args = func_get_args();
$num_args = func_num_args();
$sum = 0;
if ($num_args == 2)
{
$sum = $args[0] + $args[1];
}
elseif ($num_args == 3)
{
$sum = $args[0] + $args[1] + $args[2];
}
return $sum;
}
}

$example = new OverloadingExample();


// Calling the add method with two arguments
$result1 = $example->add(5, 10);
echo "Result with two arguments: $result1\n";
// Output: 15
// Calling the add method with three arguments
$result2 = $example->add(5, 10, 15);
echo "Result with three arguments: $result2\n";
// Output: 30

In this example, the add() method can accept variable-length argument lists using
func_get_args() and func_num_args(). Depending on the number of arguments passed, it
performs addition accordingly.

Access Specifiers:
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
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.
Protected members
A protected property or method is accessible in the class in which it is declared, as well as in
classes that extend that class. Protected members are not available outside of those two kinds of
classes. A class member can be made protected by using protected keyword in front of the
member.
The accessibility rules can be summarized by the following table −

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy