Object-oriented programming
Object-oriented programming (OOP) is a programming paradigm that focuses on the concept of objects, which can contain data and code to manipulate that data.
Overview
Object-oriented programming (OOP) is a programming paradigm that focuses on the concept of objects, which can contain data and code to manipulate that data. PHP, as a versatile programming language, supports object-oriented programming features.
Classes and Objects:
- A class is a blueprint or template that defines the properties and methods common to a particular type of object.
- An object is an instance of a class. It represents a specific entity with its own set of data and behavior.
Defining a Class:
- In PHP, you define a class using the class keyword followed by the class name.
- Properties are defined using variables within the class, and methods are defined as functions within the class.
class MyClass {
// Properties
public $property1;
private $property2;
// Methods
public function method1() {
// Code…
}
private function method2() {
// Code…
}
}
Creating Objects:
- To create an object, you use the new keyword followed by the class name and parentheses.
- This creates a new instance of the class, allowing you to access its properties and methods.
$object = new MyClass();
Accessing Properties and Methods:
- You can access properties and methods of an object using the object’s name followed by the arrow operator (->).
- Public properties and methods can be accessed from outside the class, while private properties and methods can only be accessed from within the class.
// Accessing properties
$object->property1 = ‘Hello’;
echo $object->property1;
// Calling methods
$object->method1();
Constructor and Destructor:
- A constructor is a special method that is automatically called when an object is created. It is used to initialize object properties.
- A destructor is a special method that is automatically called when an object is destroyed or goes out of scope.
class MyClass {
public function __construct() {
// Constructor code…
}
public function __destruct() {
// Destructor code…
}
}
Inheritance:
- In PHP, you can create a class that inherits the properties and methods of another class using the extends keyword.
- This allows you to reuse code and create class hierarchies.
class ChildClass extends ParentClass {
// Additional properties and methods…
}
Visibility:
- PHP supports visibility modifiers to control the access level of properties and methods.
- The three visibility modifiers are: public, protected, and private.
- public properties and methods can be accessed from anywhere.
- protected properties and methods can only be accessed from within the class or its subclasses.
- private properties and methods can only be accessed from within the class itself.
class MyClass {
public $publicProperty;
protected $protectedProperty;
private $privateProperty;
public function publicMethod() {
// Code…
}
protected function protectedMethod() {
// Code…
}
private function privateMethod() {
// Code…
}
}