Javatpoint Logo
Javatpoint Logo

PHP Classes

We first need to understand the concept of object-oriented programming, also known as OOPs, before learning about PHP classes. Unlike other programming languages like C++, JAVA, etc., PHP also supports the concept of object-oriented programming.

Object-Oriented Programming / OOPs

It is a form of programming concept where everything is considered on an object, and then we implement software with the help of using these different objects.'

For example, in a school, all students, teaching staff, administrative staff, etc., can be considered objects of a certain type called school.

A few terminologies related to OOPs and classes: -

S.No Terminology Definition
1 Class The user-defined data type combines variables, local data, and functions.
2 Objects These are local instances created by the developer to access the content of the class
3 Member Variable These are none other than variables defined inside a class and are only accessed by member functions
4 Member Function These are none other than functions defined inside a class and are generally used to access data objects
5 Inheritance It is one of the main properties of object-oriented programming, where traits and properties of a superior class are given to a subclass. The subclass inherits member function and variables present in the parent class
6 Parent class The main class generally inherits some of its traits and properties to its child class or other class. These types of classes are also known as superclasses or base classes.
7 Child class A subclass inherits some of its traits and properties from its parent class or another class, these types of classes are also known as subclasses or derived classes.
8 Polymorphism It is one of the base concepts of object-oriented programming, which states that a single function can be used for various other reasons. In this, the name of the function remains the same, but arguments can be different
9 Overloading It is a type of polymorphism where a single function class is overloaded with different forms of implementation depending on certain types of arguments, or we can overload the same type of function using different implementations.
10 Data abstraction It is a specific type of data where the data implementation details are hidden.
11 Encapsulation Process in which all data and member functions are wrapped together to create a new object.
12 Constructor A special function is automatically called when an object of the same class is formed.
13 Destructor A special function is automatically called when an object goes out of scope or gets deleted.

Define Class in PHP

PHP classes are almost like functions but with a major different that classes contain both variables and function, in a single format called object or can be said Class is made from a collection of objects

Syntax:

Parameters: -

S.No Parameters description
1 Class To declare a class, we have to use the keyword class and then the name of the class that we want to declare.
2 Variable declaration To declare a variable, we have to declare keyword var followed by $ convention and then the name of the declared variable. These are also known as member variables. These are also called properties
3 Function declaration To declare a function, we have to declare the keyword function following the name of the declared function. These are also known as member functions, but these functions are only accessible to class only. These are also called methods
4 Braces We have to enclose our class with curly braces { }

Program 1: PHP program to display the use of PHP CLASS

Output

BMW --> red
audi --> blue
volvo --> black

Here in this program, we have declared a class car, with member variables $ name and $ color. We have declared member function set _ name to add a name to a certain created object, set _ color to add color to that object, get _ name to print the object, and get _ color to print the object. To access the declared class, we have declared 3 objects $ BMW, $ Audi, and $ Volvo and called the class cars using these objects. To access the member functions declared inside the class we have used the created objects with the member functions inside the class using the value as parameters for example $ BMW - > set _ name ( " BMW " ), $ audi - > set _ color ( " blue " ), $ Volvo - > set _ name ( " Volvo " ),in order to print the result we have used the get_ name function with class objects like $ BMW - > get _ name ( ) , $ audi - > get _ color ( ).

$ this Keyword

$ this keyword refers to the present object, and this keyword can only be used inside a member function of a class. We can use $ this keyword in two ways

1) To add a value to the declared variable, we have to use $ this property inside the set _ name function

For example: -

Output

JOHN $ 2000000
ROCK $ 1200000

In the above example, we have used $ this with set_name and set_salary to add a new value to the declared variable name and salary, and later, we can get the output using echo.

2) In order to add a value to declared variable, the property of variable can be changed directly.

For example:-

Output

JOHN $ 2000000 
ROCK $ 1200000

In the above example, we have used $ this directly with name and salary to add new value to declared variable name and salary, and later, we can get the output using echo.

Program 2: PHP program to display the use of PHP CLASS

Output

JOHN
10000 
audit manager 

DOE
150000 
engineer 

NINA
70000 
accountant

In this program, we have declared a class employee to maintain a record of employees working inside a company, with member variables $ name, $ profile, and $ salary. We have declared member function set _ name to add a name to a certain created object, set _ salary to add salary to that object and set _ profile to add a profile of the employee. We have used $ this function to point towards the present object while adding the data.

To retrieve the output, we have to use the function get _ name to print the object name, get _ salary to print the object salary, and get _ profile to print the object's profile.

Creating Objects in PHP to access the class

$employee_1 = new employees;

$employee_2 = new employees;

$employee_3 = new employees;

To access class employees' member variables and member function, we have created 3 objects and assigned them to class employees using a new keyword.

Calling member functions using objects

$employee_1 -> set_name (" JOHN " );
$employee_1 -> set_salary (" 10000 " );<
$employee_1 -> set_profile (" audit manager " );

$employee_2 -> set_name (" DOE " );
$employee_2 -> set_salary ( " 150000 " );
$employee_2 -> set_profile (" engineer " );

$employee_3 -> set_name ( " NINA " );
$employee_3 -> set_salary ( " 70000 " );
$employee_3 -> set_profile ( " accountant " );

Once after creating objects we can use these objects to call the member functions inside the class, and using these member functions we can assign the values to member variables

$employee_1->get_name();
$employee_1->get_salary();
$employee_1->get_profile();

$employee_2->get_name();
$employee_2->get_salary();
$employee_2->get_profile();

$employee_3->get_name();
$employee_3->get_salary();
$employee_3->get_profile();

We have called the other member function with the declared objects to print the output.

Constructors

These are a special types of functions that are automatically called whenever an object is created. These function are created by using the _construct ( ) keyword

Syntax:

Example:

Output

the constructor class has been initiated
john
the constructor class has been initiated
doe
the constructor class has been initiated
mat

Here in this program, we have declared a class newconstructorclass, and inside the class, we have declared a new constructor using the _construct ( ) function. The main advantage of having a constructor class is that now we don't have to call the set function separately to add values to all the member variables. Now we can do the same when creating the object itself.

Destructors

These are special functions that are automatically called whenever an object goes out of scope or gets deleted. These functions are created by using the _destruct ( ) keyword

Syntax:

Example:

Output

The class "newdestructorclass" was automatically destroyed when the created object did not have a scope to initiate

Here in this program, we have declared a class newdestructorclass. We have declared a destructor inside the class using the _destruct( ) function. It does not contain any argument. It will automatically invoke whenever the object does not have scope to work with.


Next TopicFATHER OF PHP





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA