Home >Backend Development >PHP Tutorial >PHP, Classes and Objects
PHP, like Java, supports object-oriented programming and uses classes and objects as its core building blocks. Understanding these concepts is essential for mastering PHP. This guide will cover everything you need to know about classes and objects in PHP.
A class in PHP is a blueprint for creating objects. It defines the structure and behavior that the objects of the class will have.
class ClassName { // Properties (Fields) // Methods }
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
Objects are instances of classes. You create an object from a class using the new keyword.
$objectName = new ClassName();
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
Properties (also known as fields) represent the state of an object, while methods define the behavior of the object.
Properties are variables that hold the data of an object.
class Car { public $color; public $model; public $year; }
Methods are functions defined within a class that describe the behaviors of the objects.
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
Constructors are special methods that are automatically called when an object is instantiated. They initialize the newly created object.
If no constructor is defined, PHP provides a default constructor with no arguments.
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
A parameterized constructor allows you to initialize an object with specific values.
class Car { public $color; public $model; public $year; // Parameterized constructor public function __construct($color, $model, $year) { $this->color = $color; $this->model = $model; $this->year = $year; } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Main { public function run() { $myCar = new Car("Red", "Tesla", 2022); $myCar->displayInfo(); } } $main = new Main(); $main->run();
PHP does not natively support method overloading like Java, but you can simulate it using optional parameters or by handling arguments manually within a single constructor.
class Car { public $color; public $model; public $year; // Simulating constructor overloading public function __construct($color = "Unknown", $model = "Unknown", $year = 0) { $this->color = $color; $this->model = $model; $this->year = $year; } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Main { public function run() { $defaultCar = new Car(); $defaultCar->displayInfo(); $myCar = new Car("Red", "Tesla", 2022); $myCar->displayInfo(); } } $main = new Main(); $main->run();
Encapsulation in PHP is the practice of bundling data (properties) and methods (functions) within a class. It ensures the internal state of an object is safe from outside interference and misuse.
Access modifiers in PHP control the visibility and accessibility of properties, methods, and constructors. PHP supports three main access modifiers:
class ClassName { // Properties (Fields) // Methods }
Static members in PHP are associated with the class itself rather than any specific instance. They can be accessed without creating an object of the class.
Static properties are shared among all instances of a class. They are declared using the static keyword.
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
Static methods are declared using the static keyword. They belong to the class rather than an instance.
$objectName = new ClassName();
Static members are accessed using the class name, not through an object.
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
Access modifiers in PHP control the visibility of class members, ensuring encapsulation and enforcing access restrictions.
class Car { public $color; public $model; public $year; }
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
Non-access modifiers in PHP modify the behavior of class members without affecting their visibility.
class ClassName { // Properties (Fields) // Methods }
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
$objectName = new ClassName();
Inheritance in PHP is a mechanism where one class (subclass) can inherit the properties and methods of another class (superclass). It facilitates code reuse and allows for the creation of a hierarchical relationship between classes.
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
class Car { public $color; public $model; public $year; }
In this example:
Access modifiers in PHP determine the visibility of class members in subclasses and other parts of the program. They play a key role in inheritance.
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
Static members in PHP are associated with the class rather than any specific instance. They follow the same access rules as non-static members in inheritance.
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
Static methods are inherited in PHP but cannot be overridden in the same sense as instance methods. When a subclass defines a static method with the same name, it hides the parent class's static method.
class ClassName { // Properties (Fields) // Methods }
Abstract methods in PHP must be defined in abstract classes. The visibility of an abstract method in the superclass determines its visibility in subclasses. Subclasses must implement abstract methods with the same or less restrictive access modifiers.
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
Final methods in PHP cannot be overridden by subclasses, and final classes cannot be extended.
$objectName = new ClassName();
In PHP, the declaration of top-level classes (classes not nested inside other classes) follows a specific order of keywords. The declaration can include access modifiers, abstract or final keywords, and the class keyword.
class ClassName { // Properties (Fields) // Methods }
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
$objectName = new ClassName();
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
class Car { public $color; public $model; public $year; }
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Car { public $color; public $model; public $year; // Parameterized constructor public function __construct($color, $model, $year) { $this->color = $color; $this->model = $model; $this->year = $year; } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Main { public function run() { $myCar = new Car("Red", "Tesla", 2022); $myCar->displayInfo(); } } $main = new Main(); $main->run();
Abstract classes in PHP are similar to their counterparts in Java, used to define a blueprint for other classes. They contain both abstract methods (methods without implementation) and concrete methods (methods with implementation). Abstract classes are declared using the abstract keyword and cannot be instantiated directly.
An abstract class serves as a base class for derived classes. It defines common behaviors for its subclasses while allowing the implementation of specific methods in those subclasses.
To declare an abstract class in PHP, use the abstract keyword before the class keyword.
class ClassName { // Properties (Fields) // Methods }
Abstract methods are defined in the abstract class but do not have a body. Subclasses must implement all abstract methods.
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
Concrete methods in an abstract class have a body and can be inherited by the subclasses as-is or overridden.
$objectName = new ClassName();
Subclasses of an abstract class must implement all its abstract methods unless the subclass is also declared as abstract.
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
Abstract classes cannot be instantiated directly. You must use a concrete subclass to create an instance.
class Car { public $color; public $model; public $year; }
Abstract classes can have constructors, and their constructors are called when a subclass is instantiated.
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
Abstract classes can include instance variables and concrete methods, providing reusable functionality for subclasses.
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
Abstract classes can contain static methods. Static methods belong to the class and can be called without instantiating it.
class ClassName { // Properties (Fields) // Methods }
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
$objectName = new ClassName();
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
An interface in PHP defines a contract for classes that implement it. It specifies the methods a class must implement, but does not provide any method implementations itself. Interfaces allow for more flexible and modular code, enabling classes to adhere to a common set of method signatures, regardless of their inheritance hierarchy.
An interface in PHP is similar to an abstract class, but it can only define method signatures without any implementation. A class that implements an interface must provide the implementations for all methods declared in the interface. A class can implement multiple interfaces, making interfaces a key part of PHP's support for multiple inheritance of behavior.
To declare an interface, use the interface keyword. Interfaces can only contain method declarations (no method bodies), constants, and abstract methods.
class Car { public $color; public $model; public $year; }
A class that implements an interface must provide implementations for all the methods declared in the interface. A class can implement multiple interfaces, separating them with commas.
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
Methods in interfaces cannot have a body, and they must be public. When a class implements an interface, it must implement these methods exactly as declared in the interface, including the method signature.
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
A class in PHP can implement multiple interfaces. This allows for more flexibility in designing systems where classes can adhere to different contracts.
class ClassName { // Properties (Fields) // Methods }
Interfaces can contain constants. These constants are automatically public and can be accessed by any class that implements the interface.
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
An interface can extend another interface. This means that the child interface inherits all methods (signatures) from the parent interface, and can also add new methods. A class implementing the child interface must implement all methods from both the parent and child interfaces.
$objectName = new ClassName();
Interfaces cannot contain static methods. All methods declared in an interface must be instance methods. Static methods are not allowed in interfaces, as interfaces define instance-level contracts for the implementing classes.
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
class Car { public $color; public $model; public $year; }
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
The above is the detailed content of PHP, Classes and Objects. For more information, please follow other related articles on the PHP Chinese website!