Author: Saim Khalid
-
The Final Keyword
In PHP, the final keyword prevents classes and functions from being changed or overridden. It helps to keep important parts of your code secure, to guarantee no one accidentally breaks them while making changes. The “final” keyword is used in the definition of a class, a method inside a class, as well as with the definition of…
-
Encapsulation
PHP implements encapsulation, one of the important principles of OOP with access control keywords: public, private and protected. Encapsulation refers to the mechanism of keeping the data members or properties of an object away from the reach of the environment outside the class, allowing controlled access only through the methods or functions available in the class. Principle of Encapsulation…
-
Object Iteration
When working with objects in PHP, you may want to go over each property one by one. This is referred to as object iteration. It is useful when you want to verify or display all of an object’s properties without having to access them individually. A foreach loop may be employed to iterate through all the publicly…
-
Namespaces
We often organize the files in different folders. Usually a folder contains files related to a certain objective, or application or category. A folder can’t contain two files with the same name, though different folders may have a file of the same name so that the path of each file is different. The idea of…
-
Static Properties
The “static” keyword in PHP is used to define static properties and static methods in a PHP class. It may be noted that the static keyword is also used to define static variable, and static anonymous functions. Read this chapter to learn about the static properties in a PHP class. In a class definition, a…
-
Static Methods
The “static” keyword in PHP is used to define static properties and static methods in a PHP class. It may be noted that the static keyword is also used to define static variable, and static anonymous functions. This chapter discusses static methods in a PHP class. Create a Static Method in PHP In a class…
-
Traits
When designing websites with PHP, you might want to share the same functionality among different classes. However, PHP does not support multiple inheritance. A class can only inherit from one parent class. So, how do you reuse code between classes? In PHP, Traits have been introduced to overcome this limitation. You can define one or…
-
Interfaces
Just as a class is a template for its objects, an interface in PHP can be called as a template for classes. We know that when a class is instantiated, the properties and methods defined in a class are available to it. Similarly, an interface in PHP declares the methods along with their arguments and return value.…
-
Abstract Classes
An abstract class in PHP is a class that cannot be created on its own. This means you can’t create objects straight from an abstract class. Abstract classes are intended to be extended by subsequent classes. They serve as a blueprint for other classes, defining the common methods and properties that inheriting classes must implement.…
-
Class Constants
PHP allows an identifier in a class to be defined as a “class constant” with a constant value, the one that remains unchanged on a per class basis. To differentiate from a variable or property within class, the name of the constant is not prefixed with the usual “$” symbol and is defined with the…