---
title: 'Quick Explain: PHP OOPs Concepts | Fast Revision Before Interview with Practical Examples #php #oop'
source: 'https://youtube.com/watch?v=IJjVDTbIjRw'
video_id: 'IJjVDTbIjRw'
date: 2026-06-16
duration_sec: 0
---

# Quick Explain: PHP OOPs Concepts | Fast Revision Before Interview with Practical Examples #php #oop

> Source: [Quick Explain: PHP OOPs Concepts | Fast Revision Before Interview with Practical Examples #php #oop](https://youtube.com/watch?v=IJjVDTbIjRw)

## Summary

This video provides a comprehensive yet practical walkthrough of core PHP Object-Oriented Programming (OOP) concepts, specifically tailored for interview preparation. The instructor explains concepts like classes, objects, access modifiers, constructors, destructors, inheritance, traits, composition, dependency injection, and interfaces using clear, real-world analogies (e.g., a house, an e-commerce site) and live code examples.

### Key Points

- **What is OOP?** [0:16] — OOP (Object-Oriented Programming) is a programming model that focuses on objects (e.g., a car, a person). Objects have properties (attributes) and behaviors (methods). A class serves as a blueprint for creating objects.
- **Access Modifiers** [2:09] — Access modifiers (`public`, `protected`, `private`) control the visibility of properties and methods. `Public` allows access from anywhere; `protected` allows access within the class and child classes; `private` restricts access to only the same class.
- **Constructors and Destructors** [5:32] — A constructor (`__construct`) is automatically called when an object is created, used to set dynamic properties and initialize resources like database connections. A destructor (`__destruct`) runs when the object is destroyed or the script ends, mainly for cleanup (e.g., closing DB connections).
- **Inheritance** [10:27] — Inheritance allows a child class to extend a parent class, reusing public and protected properties/methods. The child can add new features or override existing ones. Single inheritance (one parent, one child) is the basic form; multilevel inheritance (a chain of classes) is also supported.
- **Protected vs. Private in Inheritance** [14:42] — `Protected` properties can be accessed by child classes (like sharing a WiFi password with family). `Private` properties cannot be accessed by child classes (like a personal locker password).
- **Multiple Inheritance Problem and Traits** [19:53] — PHP does not support multiple inheritance due to ambiguity (the diamond problem). Instead, traits (`trait` keyword) allow reusing methods across classes. The `insteadof` and `as` operators resolve method conflicts when using multiple traits.
- **Composition Over Traits** [26:56] — Composition (has-a relationship) is used when a class needs to access all functionalities of other classes. It involves creating objects of the required classes inside the class. However, composition leads to tight coupling.
- **Dependency Injection** [30:42] — Dependency injection passes dependencies (objects) from outside the class instead of creating them inside. This reduces tight coupling, improves testability, and makes the code more maintainable.
- **Interfaces** [32:54] — Interfaces define a contract (abstract methods without implementation). They enforce that certain methods must be implemented in any class that `implements` the interface. Interfaces support polymorphism and abstraction but do not provide code reusability like traits.

### Conclusion

The video systematically builds from fundamental OOP concepts (classes, objects, access modifiers) to advanced patterns (traits, composition, dependency injection, interfaces), equipping viewers with both theoretical knowledge and practical coding examples for PHP interviews.

## Transcript

Welcome to web development through practice
Today, we will cover all the important PHP
oops concepts that are commonly asked in
interviews. I’ll break them down with
practical examples so you can
confidently explain them in your
interview. Let's start with what OOP is
in PHP. OOP (Object-Oriented
Programming) is a programming model that
focuses on objects. An object can be
anything, like a car, a person, a table
a mobile phone, or anything else. The world is
naturally structured around objects,
and each object has its own properties
and behaviors. For example, a Car
Properties are Brand, color, speed, and fuel
type, and Behaviors are Start, accelerate,
brake, and refuel. for a Person, Properties
are Name, age, height, gender, and Behaviors: Walk,
talk, eat, sleep. If we look at an e-commerce
website, there are also objects like
Product, User, Shopping Cart, Order, and
Payment.
Each object has its own properties and
behaviors. now here is what Oops does. it
creates, organizes, and manages
objects to build reusable
applications.
It helps in designing software where
objects interact with each other
efficiently.  Now to organize an
object's properties and behaviors in
one place we use a class, that's why A
class acts as a blueprint for
objects. Now, let's see how this works
with a practical example!
Let's take an example of a User object
in an e-commerce website. A User object
has behaviors like registering, logging in,
logging out, and updating the profile. It
also has properties like name, email, and
password. Now, we define a User class to
organize the object's properties and
behaviors in one place. After that, inside the
User class, we define properties. To
define these properties, we use access
modifiers.
So first, let me explain what an
access modifier is. Access modifiers
control the visibility and accessibility
of properties and methods within and
outside a class. There are three types of
access modifiers in OOP: public,
protected, and private. Now, if we
define properties using the public access
modifier, we can write: public $name
= "John", public $email =
"john@example.com",
public $password = "password123".
Here, I have provided static
values for testing. Now, I create a
User class object and try to access
these properties by echoing the message and
saving the file. Then, to see the output, run the
code, and you can see that I can easily access
public properties from outside the class.
Now, let's modify the property values:
name = "Nick",
email = "nick@example.com",  and password
= "nick123".
After saving the file and running the code,
you can see that the modified values are
displayed. This means that if we use the
public access modifier, we can easily
access and modify properties or methods
from anywhere and it is not secure for
sensitive data like passwords, bank account
details, or any other private
information. Similarly,  define user class
behaviors such as register, login, logout,
and update profile, and you can add your
logic to implement these user class
behaviors. I am just showing a simple
message. These behaviors are also referred to
as functions or methods. Now, To access these
methods, we already have an object of the
user class; simply call the method by its
name. For example, call the register method and
save the file. Then run the code, and you see the
output for the register method. Similarly, you
can apply it to other methods. So here, A
class is a template that holds
properties and methods. An object is an
instance of a class. By creating an
object, we can use its properties and
methods. Now, if I change the public access
modifier to protected, remove the section
for modifying property values, and save
the file, running the code will result in an error.
This is because protected properties can
not be accessed or modified outside the
class. Similarly, If I change the protected
access modifier to private, save the file,
and run the code again, it will show an error. The
private access modifier is not
accessible outside the class. The difference
between private and protected access
modifiers is that Protected properties
can be used inside the class and in
child classes, and Private properties
can only be used inside the same class. Now,
since child classes are part of
inheritance, let’s first learn about
inheritance and then see how protected
and private properties work in
it. Before discussing inheritance, let's
first understand constructors and
destructors.
In our code, we initially defined properties
with static values. However, when using
the User class for registration, we cannot
define static data for each user. So, I
removed the static values and implemented
a constructor to set dynamic properties.
A constructor is a unique method that
automatically runs when an object is
created. Let’s see how it works. First,
I removed the existing code and added a
simple example. Inside the constructor, I
display a message "User object created!"
Then, I create an object of the User
class and save the file. When I run the code, the
message appears automatically—without calling the
constructor manually. Now let's understand
why we use a constructor? The main purpose
of a constructor is to set dynamic
properties when an object is created.
For example, during user registration,
we receive dynamic values like: name =
= 'John', email =
'john@example.com', and password =
'password123'.
When a user submits the registration
form: The form data is sent to a backend
script for validation.
Then, A User object is created, and the
user's data is passed as
arguments. Here, when we create a User
Class Object then The constructor is
called automatically and assigns these values
to user class defined properties:
this name is equal to dollar name, this
email is equal to dollaer email, dollar this
password is equal to dollar password. these
properties are now available for all methods
in the user class. For example, if we call the
register() method, it uses these values for
registration. If we then call login(), the same
values can be used for login—without
passing them again. Benefits of using a
constructor: Reusable Data: The same
properties can be used for multiple
operations (e.g., register, login, update
profile) without needing to pass
arguments to each method separately.
Other Uses of Constructors are, A
constructor can automatically connect to the
database. For example, suppose you have a
database connection file "db.php".
You can include this file at the top of
your code. Then, inside the constructor, create
an object of the database class and store
it in the '$db' property. Then, call the
connection method and store it in '$this->
conn', while also defining private '$conn'
to hold the connection. Now, you can use
'$conn' to perform database operations in
various methods of the class, such as inserting
user registration details, handling login,
and more. In PHP 8+, we can define
access modifiers (private, protected,
public) and data types directly in
constructor
parameters. So, you don't need to declare
properties separately. However, access
modifiers cannot be used inside
regular function parameters, making this a key
advantage of
constructors. Next, We can also
define strict return types for
functions. For example, first enable
strict type by declaring
"strict_types=1"
and we define an 'isLoggedIn ' function
that must return a boolean but if returns
an integer 10, PHP will throw a TypeError
because 10 is not a boolean. This is called
strict typing, ensuring better code
reliability, and improving accuracy and
consistency. Similarly, you can apply
return types to other methods, such as a
register method that returns a string as
a success message.
Now, let's discuss
destructors. A destructor is the opposite
of a constructor. It is called automatically
when a class object is destroyed or the
script ends. It is mainly used to
clean up resources, such as closing
database connections or writing logs. In our
example, we define a destructor method
and use it to close the database connection.
Once the script has executed all
previous code, the destructor will run
automatically. Save the file and you can observe
how it works after all other code has
finished
executing. Next, let’s discuss
inheritance. In simple words, inheritance
means taking or extending something from
someone else. Imagine, you don’t have a house, but
your father has one. So, you don’t need to buy the
same things that already exist in your
father’s house, such as a 'Samsung TV', 'Wooden
Table', 'Leather Sofa', and more. This means you can
use everything in your father’s house for your
needs. We can relate this situation to
Object-Oriented Programming. Here, we
have two classes: The Father class – which owns
properties like the TV, Table, and Sofa. The
You class – has no properties but can use the
items from the Father class. Since You
inherited things from your father, the Father
class is called the Parent Class, and the You
class, that inherits from it, is called the
Child Class. In OOPs, the parent class is
also known as the Base Class, and the child
class is known as the Derived Class. This is
exactly how inheritance works in
programming – the child class means you get
access to all the properties and behaviors
of the father parent class, without defining
means Buying them again. Similarly, if you want
to add new items to your father's house, you
can buy things that are not already there,
like video games. However, if you buy
something that already exists in your
father's house, such as a Samsung TV, it
replaces or overrides the one in your father’s
house item. Now, let’s see how it works with
a practical example. To implement this
functionality, I create two class files: one
for the Father class and another for the You class.
In the Father class file, I define a Father
class with properties such as public
$tv = "Samsung TV"
public $table = "Wooden Table", and
public $sofa = "Leather Sofa".
Additionally, I define methods like useTable(),
useSofa(), and watchTV(), which return simple
messages. After defining the class, I save the
Father class file. In the You class file, since
the You class does not define its own
properties, it extends the Father class to
inherit its properties and methods. To do
this, I first include the Father class file
in the You class file. Next, I use the
'extends' keyword to inherit from the Father
class. To perform an action, I create an
object of the You class, and then I call the
watchTV() method, which belongs to the Father
class. After saving the You class file and
in the output, the message from watchTV() is
displayed. This confirms that the You class
(child class) successfully accessed the
properties and methods of the parent class
(Father). Next, the You class decides to add a
new item, like a video game, extending
the items from the parent. In the You class, I
define a property called public
$newItem = "Video Game" and a
method called playGame() that returns a simple
message. Now, when I call playGame() and in the
output, the message "Playing Video Game" is
displayed. This shows that the child class can
add its own properties and methods. Next,
If the child class wants to define a
property or method that already exists in
the parent class, such as 'tv' and the watchTV()
method, it can do so by redefining them:
public $tv = "My Own
Samsung TV". Similarly, the child class
can redefine the watchTV() method. Now, when
I call watchTV(), it overrides the parent
class method, displaying the new message "I
am watching my own TV."  It is also worth
noting that if we create an object of the
parent class and try to call the same method,
watchTV(), the parent class's properties and
methods remain
unchanged. This concept is known as Single
Inheritance, where one child class
(You) derives from one parent
(Father) class. Now, Let's discuss the noted point of
how protected and private access
modifiers work with inheritance? In a Father
class, suppose there is a WiFi password
property that is initially public, meaning
anyone can access it. However, the father wants
to share it only with family members such as
with child classes, and not with
outsiders. So, We use the protected
access modifier. This ensures that only
the Father class and its child classes
like the You class can access it. To
implement this, we define a get WiFi
Password method in the You child class, which
accesses the protected WiFi password
property. When we call this method, and run the
code. you can see the output the child class
can successfully retrieve the WiFi password.
However, if another class which not related
to the Father class, such as the Neighbour class,
defines the  "get wifi password" method and
to access the father class "WiFi
password" property include the father class
file and create the object of the Father
class and return "WiFi password"
property in this method. now to test this
create an object of the neighbour class try
to access the "getWiFiPassword" method and
run the code. it shows an error because protected
properties cannot be accessed outside the
class hierarchy.
So, Protected properties Can be
accessed inside the class and in child
classes. Now, let’s say the father has a money
locker password, which is strictly
private and should not be shared with
anyone, including family members. So, We
use the private access modifier. This
ensures that only the Father class can access
it and even child classes cannot access
or modify it. now to test this we define
the "getLockerPassword" method inside the You
child class and return a simple message
with the locker password. now call this get Locker
Password method and save the file. now run the code
and the output shows an error because Can be
accessed only inside the same class not even
child classes.
Next, let's discuss when to use
inheritance in programming. Inheritance is
used to reuse code and establish a
relationship between classes, where a child
class inherits public or protected
properties and methods from a parent
class while also having the ability to
extend or modify them. Another type of
inheritance is multilevel inheritance.
This occurs when a class is derived
from another class that is already derived
from a third class. A simple example is a
family hierarchy: the "Father" class inherits
from the "Grandfather" class, and the "Son" class
inherits from the "Father" class. Here, the "Father"
class is already derived from the
"Grandfather" class, creating a multilevel
structure. As a result, the "Son" has access to
the properties and methods of both the "Grandfather"
and the "Father." Similarly, in an
e-commerce website, the "Admin" class
inherits from the "User" class, and the "Super Admin"
class inherits from the "Admin" class. In
this case, the "Super Admin" indirectly
inherits the public and protected
methods and properties of the "User" class.
Now, It's important to note that you should use
multilevel inheritance only when there is
a clear hierarchical relationship between
classes. Now, let’s implement this
concept
practically. To implement multilevel
inheritance in an e-commerce website, we have
three classes. One is a User Class which
contains common functionalities like register(), login(),
logout(), updateProfile(), and getDetails().
The second is an Admin Class, which
extends the User class and also has additional
functionalities for managing users and
products. Now, When we create an
object of the Admin class and call the get
Details() method from the User class, it
returns the admin's name and email. Here you
can notice that we reuse the getDetails()
method, as there is no need to define a
separate method to fetch admin data within the
Admin class. same as for admin register, login,
and logout we can use the same user
class methods. Now, the next class is the
Super Admin Class. This class extends the
Admin class, When we create an object
of the Super Admin class and call the get
Details() method of the user class, it returns
the name and email of the super admin. Additionally,
if we call a method from the Admin class, such
as retrieving the user list, the Super Admin
has access to all relevant data. Thus, the Super
Admin class is derived from Admin,
which is already derived from User,
forming a multilevel inheritance
structure where each class builds upon the
previous one, ensuring code reusability
and hierarchy-based access control.
The next type of inheritance is
multiple
inheritance. Here, "multiple" means that we can
extend more than one class. For example,
consider a User class that defines common
functionalities such as registering, logging in,
logging out, updating a profile, and
retrieving details. There is also an
Admin class that extends the User class
and adds its own properties and methods
for managing user data and product data.
Furthermore, we have a SuperAdmin class that
extends the Admin class and includes
its own properties and methods for
managing admin data. Now, let's
introduce another class called Seller, which
also extends the User class and has
its own properties and methods, such as
managing sales reports. Now, the Super
Admin class, which manages admin data, may
also want to manage data from the Seller
class. This is where multiple inheritance
comes into play, allowing the SuperAdmin
class to extend both the Admin and Seller
classes, using a comma to separate the class
names. However, if we try to run this code
in PHP, it will result in an error because
PHP does not support multiple inheritance.
The primary reasons are method conflicts,
ambiguity, and the diamond problem. For
example, the Admin class can access the get
Details method from the User class to
retrieve admin details by passing the admin
ID. Similarly, the Seller class can call the get
Details method from the User class by
passing the seller ID. If the SuperAdmin class
attempts to call the getDetails method
from the User class, it will encounter two
identical copies of the method—one from the
Admin class and one from the Seller class.
This creates the diamond problem. Additionally,
if the Admin class has a method called manage
Order for approving or rejecting
orders, and the Seller class has its own
manageOrder method for processing customer
orders, a conflict arises. When the
SuperAdmin class tries to access the manage
Order method, ambiguity occurs because it is
unclear which method should be executed:
the one from the Admin class or the one from the
Seller class. To resolve these issues, use traits
or composition instead of using multiple
inheritance. A trait is a way to define
methods that can be reused across multiple
classes. In our example, the SuperAdmin
class wants to access the functionalities of
both the Admin and Seller classes to manage the
system. Since we cannot use multiple
inheritance, we create separate trait files:
one for Admin called AdminTrait and another
for Seller called SellerTrait, and remove the
extensions to Admin and Seller classes
from SuperAdmin. In the AdminTrait file,
we define the trait using the trait keyword
and name it AdminTrait. In the SellerTrait
file, we do the same and name it SellerTrait.
Now, if we convert all the functionalities of the
Admin and Seller classes into traits, we
won't be able to access the User class
functionalities because both the Admin and Seller
classes extend the User class. Therefore,
we cannot convert the entire class into
traits. We do not make any changes to the
Admin and Seller class files. Instead, we
extract only the reusable code from the
Admin and Seller classes, such as the manage
Order method. To do this in AdminTrait, we
define the manageOrder method from the Admin
class and remove the existing manageOrder
method from the Admin class. Similarly, in
SellerTrait, we define the manageOrder method
from the Seller class and remove it from the
Seller class as well.
You may wonder why we create separate trait
files for Admin and Seller. We could create
one common trait file, such as OrderTrait, and
define the `manageOrder` method there, passing a
role type to fetch order records. However,
if we need to change the logic or update
functionalities for Admin in the future, it
might affect the Seller's manageOrder method,
leading to tight coupling, and If the Admin
needs to use that common trait file, it will
also include the Seller's code, which is not
ideal. This is why we create separate
traits for Admin and Seller, where we
define common functionalities that can be
used in multiple classes. After defining
the traits, we include the AdminTrait and
SellerTrait files in the SuperAdmin class,
and We then use AdminTrait and SellerTrait
with the `use` keyword. Next, we define a
new method called getAdminOrders, which
calls the `manageOrder` method from Admin
Trait. Similarly, we define a method called
getSellerOrders, which calls the manage
Order` method from SellerTrait. At this point,
we face a challenge. How does SuperAdmin know which
manageOrder method belongs to AdminTrait
and which belongs to SellerTrait? To resolve
this issue, we can use the 'insteadof' and 'as'
operators. Before implementing this, note that
the Super Admin class does not extend the
the User class. As a result, we cannot access
the name properties of the User class in the
manage Admins method, which will lead to an
error. Therefore, please remove the manageAdmin
method from the superAdmin class and also
remove arguments from the superAdmin
class object. Next, in the use keyword
define AdminTrait with the scope
resolution operator for the manageOrder
method instead of SellerTrait. So, when you
call the manageOrder method, it comes from the
AdminTrait. Then, define SellerTrait and
use the scope resolution operator to rename
its manageOrder method as manage Order
As Seller. This way, when you call manage Order
As Seller, it comes from SellerTrait. Now,
in the getAdminOrders method, there is no
need to change the manageOrder method because
we use the insteadof keyword. This means that
if both AdminTrait and SellerTrait have a
manage Order method, it always comes from
AdminTrait. At the same time, we rename the
manageOrder method from SellerTrait as
manageOrderAsSeller, so in the get Seller
Orders method, we call manageOrderAsSeller.
Next, in the SuperAdmin object, when
you call the getAdminOrders method, it
returns results from AdminTrait. If
you call the getSellerOrders method, it
returns results from SellerTrait.
However, the issue is not yet resolved because the
SuperAdmin class should have all functionalities
of both Admin and Seller classes, not just the
common
functionalities. For example, if SuperAdmin
needs to manage admin users and access
seller sales reports, we cannot use
traits. Instead, we use composition, which
follows a "has-a" relationship between classes.
For example, the SuperAdmin class has
Admin and Seller class
functionalities. We achieve this by creating
objects of Admin and Seller classes
inside the SuperAdmin class. Before
implementing composition, we remove the trait
code of AdminTrait and SellerTrait
otherwise it will show an error.
To implement composition by creating
objects of the Admin and Seller classes
within the SuperAdmin class, we start by
including the files for the Admin and
Seller classes in the SuperAdmin class.
Next, we define properties such as
private $admin to store the Admin
class object and private $seller to
store the Seller class object. We then
create a constructor where we instantiate
an object of the Admin class and assign
it to the $admin property. Now, if we
check the Admin class, we see that it
extends the User class, and the User class
has a constructor that requires a name,
email, and password. However, in the SuperAdmin
class, when creating an Admin object,
SuperAdmin does not have the admin password. To
resolve this, in the SuperAdmin class
object, we pass the admin name as "Admin
User", the email as "admin@example.com", and
the password as null. In the SuperAdmin class
constructor, we receive these details as
parameters, such as $adminName,
$adminEmail, and $adminPassword
equal to null. Then, we pass $adminName,
$adminEmail, and $adminPassword
into the Admin class object. Similarly, if
we check the Seller class, it also extends
the User class, which requires a name,
email, and password in its constructor.
When creating an object of the Seller
class, SuperAdmin does not have the seller’s
password. To resolve this, in the SuperAdmin
class object, we pass the seller's name as
"SellerUser", the email as "seller@example.com",
and the password as null. The constructor
receives these parameters as $sellerName,
$sellerEmail, and $sellerPassword is
equal to null. then passes them into the Seller
class object, assigning it to the
$seller property. This approach allows us to
access functionalities from both the Admin
and Seller classes. For example, we define
a method called get Admin Orders, which
returns the result of the manage Orders
method from the Admin class object.
Similarly, we define a method called get Seller
Orders, which returns the result of the
manage Orders method from the Seller class
object. When we call the get Admin
Orders method, the SuperAdmin class
successfully accesses the Admin functionalities.
Likewise, when we call the get Seller
Orders method, we receive the result from the
Seller class. While this approach allows
access to various functionalities of the Admin
and Seller classes, it has a drawback: we can
not perform unit testing on the Super
Admin class because it directly depends
on the Admin and Seller classes. This creates
tight coupling, meaning that SuperAdmin
depends heavily on Admin and Seller, making
future modifications challenging. To resolve
this issue, we use Dependency Injection.
Dependency Injection means injecting
dependencies rather than creating them
inside a class. For example, instead of the
SuperAdmin class creating its
dependencies, such as Admin and Seller class
objects, they are passed to the SuperAdmin
class from the outside. Let's see how to
achieve this: First, move the Admin class
object creation outside the SuperAdmin
class and store it in the $admin
variable. Similarly, move the Seller class
object creation outside the SuperAdmin
class and store it in the $seller variable.
Next, when creating the SuperAdmin
class object, remove the parameters and
instead pass the $admin and $seller
variables. Now, in the SuperAdmin class
constructor, modify the parameters to
accept Admin $admin and Seller
$seller. Here, the Admin $admin and Seller
$seller parameters use type hinting,
meaning that PHP ensures that the
arguments passed to the constructor must be
objects of the Admin and Seller classes.
Next, assign $admin to the SuperAdmin
class's $admin property so it can
be used within the class. Similarly, assign
$seller to the SuperAdmin class's
$seller property for use within the class.
Since the get Admin Orders and get Seller
Orders methods already use these properties,
no further changes are needed. Now, when calling
the get Admin Orders method and running the code,
you see that the SuperAdmin class successfully
accesses Admin class
functionalities. Similarly, when calling the get
Seller Orders method and running the code, the Super
Admin class successfully accesses Seller class
functionalities. This approach reduces tight
coupling between classes and makes
unit testing, scalability, and
maintainability
easier. So far, we have covered classes,
objects properties methods access
modifiers constructors destructors
single inheritance, multiple inheritance,
traits, composition, dependency injection, and
method
overriding. Now, let's discuss interfaces. We
continue with the same example, where we have a
User class, an Admin class, a Seller class,
and a SuperAdmin class. The Admin and Seller
classes extend the User class, and the Super
class needs to access functionalities
of both Admin and Seller. We have already
achieved this using composition and
dependency injection. We also tried
using Admin Trait and Seller Trait, but
since traits are used for common
functionalities, Traits do not solve the multiple
inheritance issue. Now, let's try
interfaces. The goal is to solve the multiple
inheritance issue, meaning Super Admin should
be able to access Admin and Seller
functionalities. First, we create an Admin
Interface file for the Admin class and
define the interface using the interface keyword.
Similarly, we create a Seller Interface file
for the Seller class and define the Seller
Interface. Now, If we convert the Admin class
into Admin Interface, the Admin class cannot
inherit from the User class. The same applies
to the Seller class. So, converting an entire
class into an interface is not a good
idea. So, let's see if interfaces can be
used for code reusability.
Suppose both Admin and Seller classes have a
manage Orders method but with own logic. We
move the Manage Orders method of the admin
class into Admin Interface and the Manage
Orders method of the seller class into Seller
Interface. However, in interfaces, methods can
not have logic, which means interface methods
must be empty, which is only defined,
not implemented, and these methods are known
as abstract methods. Next, if we
include Admin Interface and Seller Interface
in the SuperAdmin class, we cannot use them
directly. Interfaces must be
implemented by a class. so, if Super
Admin implements Admin Interface, it must
define the manage Orders method with its own
logic. However, if SuperAdmin also
implements Seller Interface, there will be a
method name conflict because both interfaces
contain manage Orders. So, if interfaces do
not solve multiple inheritance issues and
do not provide code reusability like
traits, why use them? The answer is that
interfaces help create a contract in
a project. For example, if we create a
Manage Orders Interface as a contract
for Admin, Seller, and SuperAdmin classes,
then we must implement this interface and
define the manage Orders method in these
classes. If any class forgets to define
this method, PHP will show an error. So, note the
point that interfaces do not allow
properties, and all methods must be public
and abstract methods. Interfaces are
useful when you need to ensure specific
methods exist in different classes with
different
implementations. Next, interfaces enable
polymorphism abstraction encapsulation
and inheritance.
These concepts will be explained in the
next video.
