UNIT 4php
UNIT 4php
The keyword class is followed by the name of the class that you want to
Department of BCA, SRNMNCPage 1
define. Class name follows the same naming conventions as used for a PHP
variable. It is followed by a pair of braces enclosing any number of variable
declarations (properties) and function definitions.
Variable declarations start with reserved keyword 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. Functions inside a
class are also called methods.
In PHP, instead of the var keyword (which is now considered deprecated), you
should use visibility modifiers to define properties within a class. The three
primary visibility modifiers are public, protected, and private.
Example:
class Student {
public $name;
public $age;
This Keyword:
The this keyword in PHP is used within a class to refer to the current instance
of the class, enabling access to the class's properties and methods from
within its own methods.
It allows you to distinguish between class-level (static) and instance-level
Department of BCA, SRNMNCPage 2
(non-static) members, ensuring that the properties and methods accessed
belong to the specific object instance.
This keyword is essential for maintaining context, as it differentiates between
the current object's properties and those of other instances or the class itself.
By using this, you enhance the readability and maintainability of your code,
clearly indicating that certain properties and methods are part of the current
object instance.
4.1.2 Visibility Modifiers:
In PHP, classes use visibility modifiers to control the accessibility of their
properties and methods. There are three visibility modifiers:
1. Public:
Properties and methods declared as public can be accessed
from anywhere, including from outside the class, within the
class itself, and by child classes.
Public properties and methods are typically used for class
components that need to be accessible or modifiable from
outside the class. For instance, public methods can serve as
interfaces to manipulate private or protected data within the
class.
2. Protected:
Properties and methods declared as protected can be accessed
within the class itself and by any subclass that extends the
parent class. They are not accessible from outside the class
hierarchy.
Protected properties and methods allow subclasses to interact
with the parent class’s internals while preventing external
access. This facilitates a controlled inheritance structure.
3. Private:
Properties and methods declared as private can only be
accessed within the class that defines them. They are not
accessible from outside the class or by any subclass.
Private properties and methods encapsulate data and behavior
that should not be exposed or altered by external entities,
including subclasses. This is ideal for sensitive data or internal
Department of BCA, SRNMNCPage 3
logic requiring strict control.
4.2 Objects
An Object is an individual instance of the data structure defined by a class.
We define a class once and then make many objects that belong to it. Objects
are also known as instances.
When a class is defined, it acts as a blueprint for creating multiple objects
that share the same properties and behaviours but hold their own individual
states.
For example, consider a class Student that includes attributes such as name,
age, and student ID, and methods like enroll() and displayDetails(). Each
object created from this class represents an individual student with unique
values for these attributes. For instance, one Student object might have the
name "Alice", age 20, and student ID "S123", while another might be "Bob", age
22, and student ID "S124".
These objects encapsulate data and functions, enabling modularity and reuse
in programming. As such, objects are also known as instances, highlighting
their role as concrete manifestations of the abstract class definition.
The syntax for creating objects is as follows:
Objectname=new classname();.
In PHP, creating an object from a class involves calling the class constructor
using the new keyword, which allocates memory for the object and initializes
its variables..
Each object instantiated from a class operates independently, allowing
multiple instances to coexist with their own unique states and behaviours.
An object is a data type that can store data and functions together in a single
unit. Objects are created using the new keyword followed by the class name.
Once an object is created, you can access its properties and methods using
the arrow notation (->).
A class can have properties, which are variables that store data, and methods,
which are functions that perform actions on the object. Classes can also have
constructors, which are special methods that are called when an object is
created, and destructors, which are special methods that are called when the
Department of BCA, SRNMNCPage 4
object is destroyed.
<?php
class Student {
public $name;
public $age;
$student1->read("aaa", 20);
$student1->display();
$student2->read("bbb", 22);
$student2->display();
?>>
Parameterized Constructor:
A parameterized constructor in PHP is a special method called __construct
that initializes an object with parameters.
When an object of a class is created, the constructor method is automatically
called, allowing you to set initial values for object properties.
Example
<?php
class Student {
public $name;
Department of BCA, SRNMNCPage 6
public $age;
$student1->read("aaa", 20);
$student1->display();
$student2->read("bbb", 22);
$student2->display();
?>>
4.3.2 Destructors:
A destructor is called when the object is destructed or the script is stopped or
exited.
If you create a __destruct() function, PHP will automatically call this function
at the end of the script.
Example:
<?php
Class student
{
public $name;
public $regno;
Department of BCA, SRNMNCPage 7
function __construct($name, $regno) {
$this->name = $name;
$this->regno = $age;
}
Function __destruct(){
echo “exiting script , destructing objects”;
}
}
$s=new student(“AAA”,”b123”);
4.5 Inheritace:
Inheritance in object-oriented programming is a fundamental concept where
one class (the child or subclass) can inherit properties and methods from
another class (the parent or superclass).
This relationship allows for the reuse of code, as the child class can access
and extend the behaviours defined in its parent class. Inheritance facilitates
the creation of hierarchical structures, where classes can be organized based
on their similarities and differences. The subclass inherits all non-private
members (properties and methods) from its superclass, enabling developers
to build upon existing functionality without needing to rewrite cod
This principle promotes code reusability, modularity, and the efficient
management of complex systems by leveraging a structured and hierarchical
approach to software design.
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.
PHP supports only single inheritance, where only one class can be derived
from single parent class.
Example: