What is OOP in PHP?
Object-oriented programming is known as OOP.
While object-oriented programming involves constructing objects that include both data and functions, procedural programming involves developing procedures or functions that perform actions on the data.
Programming in an object-oriented manner has a number of benefits over procedural programming:
- OOP is quicker and simpler to use.
- OOP gives the programmes a clear structure.
- OOP makes it possible to design fully reusable applications with less code and quicker development time by encouraging PHP programmers to \”Don\’t Repeat Yourself\” (DRY) in their code and makes the code easier to maintain, modify and debug.
- OOP makes it possible to create full reusable applications with less code and shorter development time
We can think of our universe as being made up of things like the sun, earth, moon, etc. In the same way, we can think of our car as being made up of parts like wheels, steering, gears, etc. In the same way, there are object-oriented programming concepts that treat everything as an object and use different objects to build software.
In the next chapters, we will learn more about OOP in details.
Concepts About Objects
Before we go into more detail, let\’s define some important terms related to Object-Oriented Programming.
Class: This is a type of data that was made by the programmer. It has both local functions and local data. You can think of a class as a plan for making many different objects of the same type.
Object: A single instance of the data structure that a class sets up. You only have to define a class once, and then you can make many objects that are part of it. Objects can also be referred to as instances.
Member variables: Member variables are variables that are set up inside a class. The outside of the class won\’t be able to see this data, but member functions can be used to get to it. When an object is made, these variables are called \”attributes of the object.\”
Member function: This is a function that is defined inside a class and is used to get to an object\’s data.
Inheritance: Inheritance is the process by which a class is made by taking over the functions of its parent class. Here, a child class will get some or all of a parent class\’s member functions and variables.
Parent class: A class from which another class gets its traits. This is also known as a super class or base class.
child class: A class that gets its traits from another class is called a \”child class.\” This is also called a \”derived class\” or \”subclass.\”
Polymorphism: Polymorphism is an object-oriented idea that says the same function can be used in different ways. For example, the name of a function will stay the same, but it will take a different number of arguments and be able to do different things.
Overloading: Overloading is a type of polymorphism in which the way some or all operators work depends on the types of their arguments. In the same way, functions can be overloaded in different ways.
Data abstraction: Data abstraction is any way of showing data in which the details of how it is used are hidden (abstracted).
Encapsulation: Encapsulation is the idea that all the data and member functions of an object should be wrapped up in a single package.
Constructor: Constructor is a special kind of function that is automatically called whenever an object is made from a class.
Destructor: Destructor is a special kind of function that will be called automatically whenever an object is deleted or goes out of scope.
Defining PHP Classes
In general, this is how you define a new class in PHP:
Here is how each line is described:
The name of the class you want to define, followed by the special form class.
A group of braces that surrounds a variable declaration or a function definition.
Variable declarations start with the special form var, which is followed by a standard $ variable name. Variables can also have their initial value set to a constant value.
Function definitions look a lot like PHP functions that don\’t belong to a class, but they are part of the class and will be used to set and get data from objects.
Putting objects together in PHP
Once you\’ve set up your class, you can make as many objects of that class type as you want. Here is an example of how to use the new operator to create an object. Next, we\’ll look at how to use member functions and variables.
Call Functions of a Member
After you\’ve made your objects, you can call member functions that are related to that object. Only member variables of the related object will be able to be processed by a member function.
Functions of Constructors
Constructor Functions are a special kind of function that are automatically called when an object is created. So, we make the most of this behaviour by using function Object() { [native code] } functions to set up a lot of things.
PHP provides a special function called __construct() to define a function Object() { [native code] }. You can give the function Object() { [native code] } function as many arguments as you want.
The following example will make one function Object() { [native code] } for the Books class, which will set the book\’s price and title when the object is made.
We no longer have to call the set function twice to change the price and title. We can only set these two member variables when the object is first created.
Destructor
destruct can be used to define a destructor function, just like it can be used to define a function Object() { [native code] } function (). In a destructor, you can let go of all the resources.
Inheritance
Using the extends clause, a PHP class definition can choose to inherit from a parent class definition. Here\’s how it\’s put together:
Because of inheritance, the child class (also called a subclass or derived class) has these traits:
all the member variable declarations of the parent class are added automatically.
Automatically has all the same member functions as the parent, which (by default) work the same way they do in the parent.
Now, the class Novel keeps two more member functions on top of the ones it inherited.
Function Taking Priority
Definitions of the same name in parent classes are overridden by definitions of the same name in child classes. A function that is passed down from a parent class can have its definition changed in a child class.
The general public
Unless you say otherwise, a class\’s properties and methods are public unless you say otherwise. In other words, you can get to them in three different ways.
- From outside the class where it is declared
- From within the class that declares it
- From within another class that implements the class where it is declared
Up until now, we\’ve seen everyone as a public member. If you don\’t want everyone to be able to see the members of a class, you can make them private or protected.
Private member
By making a member private, you keep it from being used outside of the class where it was declared. The private member can\’t be accessed from outside the class or from classes that inherit the class where it\’s declared.
You can make a class member private by putting the keyword private in front of the member.
When another class uses extends to take over the MyClass class, both myPublicFunction() and $driver will be seen. Because myPrivateFunction and $car are marked as private, the class that extends won\’t know about them or be able to use them.
Protected Members
A protected property or method can be used in the class where it was declared and in classes that extend that class. Protected members can\’t be found in any other classes than those two. You can make a class member protected by putting the keyword protected in front of the member.
Interfaces are set up so that implementers can use the same names for functions. These interfaces can be used by different implementors based on their needs. You could say that interfaces are the bones that developers fill in.
Constants
A constant is kind of like a variable because it has a value, but it is more like a function because it can\’t change. Once you declare a constant, it does not change.
It\’s easy to declare one constant, which is what this version of MyClass does.
In this class, requiredMargin is a constant. It is declared with the keyword const, and under no circumstances can it be changed to anything other than 1.7. Note that the constant\’s name does not have a leading $, as variable names do.
Abstract Classes
An abstract class is one that cannot be instantiated, only inherited. You declare an abstract class with the keyword abstract, like this −
When inheriting from an abstract class, all methods marked abstract in the parent\’s class declaration must be defined by the child; additionally, these methods must be defined with the same visibility.
Note that function definitions inside an abstract class must also be preceded by the keyword abstract. It is not legal to have abstract function definitions inside a non-abstract class.
Static Keyword
Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can) (though a static method can).
Final Keyword
PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
Following example results in Fatal error: Cannot override final method BaseClass::moreTesting()
Calling parent constructors
Instead of writing an entirely new function Object() { [native code] } for the subclass, let\’s write it by calling the parent\’s function Object() { [native code] } explicitly and then doing whatever is necessary in addition for instantiation of the subclass.