Unit 3bskbaskj
Unit 3bskbaskj
Objects :
An object is an instance of a class.
Object is one type of class variable.
An object is represented as its properties (attributes) and the operation performed on it.
We can create multiple objects from a class. Each object has all the properties and methods defined in the
class, but they will have different property values.
Example: suppose Student is a class which contains two objects s1 and s2 with properties marks and
percentage.
Classes :
Class is a collection of objects of similar type.
Object with same properties and operations form a group known as class.
Class contains data member and data function.
In a class, variables are called properties (data member) and data functions are called methods.
Example: Suppose class name is car, then we can create three individual objects with the name
of: Mercedes, Bmw, and Audi.
Properties:
Properties are variables that are defined within a class. These variables are then used by the methods,
objects of the class.
Properties can accept values like strings, integers, and booleans (true/false values), like any other
variable.
In PHP, a property is qualified by one of the access specifier keywords, public, private or protected.
Name of property could be any valid label in PHP.
Value of property can be different for each instance of class.
Property is available to object with the help of -> operator.
Example:
<?php
class Student
{
// Properties
public $name;
}
Methods:
The classes most often contain functions. A function inside a class is called a method.
These functions can then be called from an object.
Functions can be public, private or protected. By default is public.
Example:
class Student {
// Properties
public $name;
// Methods
function set_name($name)
Access modifiers:
Properties and methods can have access modifiers which control where they can be accessed.
There are three access modifiers:
public - the property or method can be accessed from everywhere. This is default
protected - the property or method can be accessed within the class and by classes derived from that
class
private - the property or method can ONLY be accessed within the class
<?php
class Classname {
// code goes here...
}
?>
we declare a class named Student consisting of one property ($name) and two methods set_name() and
get_name() for setting and getting the $name property:
<?php
class Student {
// Properties
public $name;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
?>
Prepared by: Department of Computer Engineering Page 2
Subject Name: Introduction to Web Development Unit No: 3 Subject Code: 4340704
Object:
Objects of a class are created using the new keyword.
We can create multiple objects from a class. Each object has all the properties and methods defined in the
class, but they will have different property values.
<?php 30
class Add {
protected $a;
protected $b;
function set_no($a,$b) {
$this->a = $a;
$this->b = $b;
}
function sum() {
return $this->a + $this->b;
}
}
echo $no->sum();
echo "<br>";
?>
function __construct(){
$this->height=0;
$this->width=0;
}
function show(){
echo "Height=$this->height Width=$this-
>width";
}
}
$obj=new rectangle();
$obj->show();
?>
Destructors
Destructors are called when an object destructs. Usually, it is when the script ends .
A destructor is a public method which is named as __ destruct
Syntax:
function __destruct()
{
// destroying the object or clean up resources here
}
Example: Output:
<?php Object created
class MyClass { Object destroyed
public function __construct() {
echo "Object created"."<br>";
}
3.4. Inheritance
It is the process the by which object of one class derived (acquired) the properties of object of another
class.
In inheritance, the old class is called as the base class and the new class is called as the derived class or
subclass.
Inheritance provides you with many benefits that make PHP programming a lot more convenient.
One such benefit is code reusability.
Reusability allows you to generate clean code and the replication of code gets reduced to almost zero.
Reusing existing codes serves various advantages.
Single Inheritance: There is only one base class and one sub/derived class in a single inheritance and the
subclass is directly inherited from the base class.
Hierarchical Inheritance: the hierarchical inheritance adopts a tree-like structure, where multiple derived
classes are inherited from the base class.
Multilevel Inheritance: Multilevel occurs at different levels. one base class is inherited by a derived class,
then that derived class is inherited by other derived classes, and so on.
where, ChildClass is a derived class (also called extended class and subclass) that extends ParentClass (also
called superclass and base class).
Single inheritance: A derived class with only one base Class is called Single Inheritance.
Here, A is a base class and B is a derived class. Class B will acquire all the members declared in Class A.
Example : 1
<?php
class ParentClass
{
function a()
{
echo "A member function of the base class.<br>";
}
}
$Obj->b();
?>
Output:
member function of the base class.
A member function of the child class.
Multilevel Inheritance: A chain process of deriving a class from another ‘derived class’ is called multilevel
inheritance.
Here, Class A is a Base Class, class C is a Derived Class and class B is a intermediate Base Class.
Class C inherits from class B and class B inherits from class A.
So, class C will acquire all the members of class B as well as class A.
Example 1:
<?php
class ParentClass
{
function a()
{
echo "A member function of the base class.<br>";
}
}
?>
Output:
<?php
class Area
{
if($name == 'area')
{
switch(count($arg))
{
case 0 : return 0 ;
case 1 : return 3.14 * $arg[0] ;
case 2 : return $arg[0] * $arg[1];
}
}
}
$c = new Area();
echo "Area of circle:".$c->area(5)."</br>";
$rect = new Area();
echo "Area of rectangle:".$rect->area(5,10);
?>
Area of circle:15.7
Area of rectangle:50
function overriding: The two methods with the same name and same parameter is called overriding.
Both parent and child classes should have same function name with and number of arguments.
It is used to replace parent method in child class. The purpose of overriding is to change the behavior
of parent class method.
<?php
class Base
{
function display()
{
echo "Base class function :display <br>";
}
function demo()
{
echo "Base class function:demo<br>";
}
}
class Derived extends Base
{
function demo()
{
echo "Derived class function:demo<br>";
}
}
$ob = new Base;
$ob->demo();
$ob->display();
$ob1 = new Derived;
$ob1->demo();
$ob1->display();
?>
Output:
Base class function: demo
Base class function :display
Derived class function: demo
Base class function :display
3.6. Interface
Interfaces allow you to specify what methods a class should implement.
Interfaces are declared with the interface keyword.
All methods declared in an interface must be public.
To implement an interface, a class must use the implements keyword.
A class that implements an interface must implement all of the interface's methods.
<?php
interface MyInterface
{
?>
Output:
Method1 Called
Method2 Called
Output:
Derived class
class base
{
final public function display()
{
echo "Base class..";
}
}
class derived extends base
{
public function display()
{
echo "derived class";
}
}
$obj = new derived();
$obj->display();
?>
Output:
<?php
class Student {
public $name;
public $sem;
public $spi;
}
$obj->name = "abc";
$obj->sem = "3";
$obj->spi = "9.2";
$copy->name = "xyz";
$copy->sem = "3";
$copy->spi = "8.7";
?>
Output:
abc 3 9.2
xyz 3 8.7