Defining a Class in PHP
An object in programming has two characteristics: state and behavior. The state in an object is stored in variables that are referred to as properties. The behavior in an object consists of functions or methods. In order for an object to be created, there must be a blueprint or definition of the object. This blueprint, in programming, is called a class. In this article, we will discuss what is involved in defining a class.
Before discussing defining a class, we will briefly discuss what Object-Oriented programming is.Objects and classes are used in Object Object-oriented programming has several advantages over procedural programming. It provides a clear modular structure for programs. It makes easier to create more complicated behavior with less code. Object-oriented programming is based on the concept of objects. In object-oriented programming, objects are defined with its own properties or attributes. Each object can also contain its own procedures or methods. The four principles of object-oriented programming are encapsulation, abstraction, inheritance, and polymorphism. The principle of encapsulation entails that all the properties and methods of an object is kept private and safe from interference by other objects. In each object we can have both private and public variables and methods. Private variables and methods cannot be called or used by other objects, whereas public ones can. Abstraction means that every object only exposes a high-level methods for using it. The code within is irrelevant to other objects interacting with the object. With inheritance, we can reuse common logic in one class to create another class. With inheritance we extract the logic in one object, called the parent, to another object, called the child. Polymorphism gives us a way to use an object exactly like its parent but keeping its own methods as they are. This resolves the problem when the child might have a different way of implementing a method.
Before an object can be created, a class has to be defined. A class acts as a template or blueprint from which lots of objects can be created. When individual objects are created, they inherit the same generic properties and behaviors. Each object can have different values for certain properties. Think about a blueprint for a chair. We can build several chairs from the same blueprint with each chair being different sizes, colors and shapes. In PHP, we declare a class with the following syntax:
class ClassName
{
}
Class member variables are called properties. Type declaration was introduced in PHP 7.4.0 where you can declare the data type. Typed properties must be initialized before accessing or you will receive an error. Within the class, properties may be accessed by using $this->property. A special type of property for the class is the static property. The static property is defined using the keyword static. Static properties are accessible without having to instantiate the class. Static properties can also be accessed within an instantiated class object. Another type of class properties is the constant property. The property is declared constant with the const keyword. The constant property doesn't change regardless of the object but can be redefined by a child class.
Class member functions are called methods. Within the class, methods can be accessed by using $this->method(). A special type of method for the class is the static method. The static method is defined using the keyword static. Static methods are accessible without having to instantiate the class. Static properties can also be accessed within an instantiated class object. Methods are declared in PHP with the keyword function.
To set the rights for class methods and variables we use access modifiers which are PHP keywords. The keywords are public, private and protected. Public class members are accessible from anywhere, even from outside of the class scope. Private class members can only be accessed from within the class itself. Protected class members are the same as private class members with one exception, the class members defined as protected can still be accessed from its subclass ( classes that inherit from this class). In PHP, if you don't specify any access modifiers, the class members default to public class members.