Object-oriented programming (OOP) is a programming paradigm that represents and manipulates data and behavior using objects, which are instances of classes. PHP provides OOP support via a collection of built-in functions and structures.
A class in PHP is a blueprint for constructing objects. A class defines an object’s properties (also known as member variables or attributes) and methods (also known as member functions or behaviors). The terms “var” and “function” are used within the class to specify properties and methods, respectively. Here’s an example of a basic PHP class:
class Person {
var $name;
var $age;
function setName($name) {
$this->name = $name;
}
function getName() {
return $this->name;
}
function setAge($age) {
$this->age = $age;
}
function getAge() {
return $this->age;
}
}
The class “Person” in this example contains two properties: $name and $age. There are four methods as well: setName(), getName(), setAge(), and getAge(). These methods are used to set and retrieve property values. It should be noted that the term “this” refers to the current object within the class.
To make a class object, we use the “new” keyword followed by the class name. Here’s an example:
$person = new Person();
Once we have an object, we can use the methods of the class to set and get the values of the properties:
$person->setName("John Doe");
$person->setAge(30);
echo $person->getName(); // prints "John Doe"
echo $person->getAge(); // prints "30"
Inheritance is a fundamental concept in object-oriented programming that allows one class to inherit attributes and functions from another. A subclass or derived class is one that inherits from another class, whereas the class from which it inherits is known as the superclass or base class.
Here’s an example of a “Student” subclass that derives from the superclass “Person”:
class Student extends Person {
var $studentId;
function setStudentId($id) {
$this->studentId = $id;
}
function getStudentId() {
return $this->studentId;
}
}
In this example, the Student class inherits all properties and methods of the Person class and adds a new property $studentId and methods setStudentId(), and getStudentId() to it.
$student = new Student();
$student->setName("Jane Doe");
$student->setAge(20);
$student->setStudentId("123456");
echo $student->getName(); // prints "Jane Doe"
echo $student->getAge(); // prints "20"
echo $student->getStudentId(); // prints "123456"
Interfaces are a type of contract that specifies a set of methods that a class must implement. The interface keyword is used to define an interface, and it can only include method signatures, not method bodies.
Here is an example of a “Shape” interface that defines a set of methods for shapes:
interface Shape {
public function getArea();
public function getPerimeter();
}
A class that implements an interface must define the methods specified in the interface. Here’s an example of a class “Rectangle” that implements the Shape interface:
class Rectangle implements Shape {
private $width;
private $height;
public function __construct($width, $height) {
$this->width = $width;
$this->height = $height;
}
public function getArea() {
return $this->width * $this->height;
}
public function getPerimeter() {
return 2 * ($this->width + $this->height);
}
}
In this example, the Rectangle class implements the Shape interface’s getArea() and getPerimeter() functions. We can now build a Rectangle object and use its methods:
$rectangle = new Rectangle(5, 10);
echo $rectangle->getArea(); // prints "50"
echo $rectangle->getPerimeter(); // prints "30"
Classes, objects, inheritance, and interfaces are the fundamental ideas of OOP in PHP. There are many additional sophisticated principles that may be applied in PHP to make the software more modular, manageable, and reusable, such as abstract classes, polymorphism, encapsulation, and so on.
It is crucial to remember that OOP is only one paradigm in PHP; there are many other methods to construct PHP code; yet, OOP is extensively used and regarded a recommended practice in current PHP development.
Q&A
Q: What is object-oriented programming (OOP)?
A: OOP is a programming paradigm that represents and manipulates data and behavior using objects, which are instances of classes.
Q: Does PHP support OOP?
A: Yes, PHP is a popular server-side scripting language that includes built-in functions and features to enable OOP.
Q: How do I create an object in PHP?
A: To create an object of a class, we use the “new” keyword, followed by the class name. For example: $person = new Person();
Q: What is inheritance in OOP?
A: Inheritance is a fundamental aspect of OOP that allows a class to inherit properties and methods from another class.
Q: Can you explain what interfaces are?
A: Interfaces are a way to define a contract for a class, specifying a set of methods that the class must implement.
Q: Are there other advanced concepts of OOP in PHP?
A: Yes, there are many additional sophisticated principles that may be applied in PHP to make the program more modular, manageable, and reusable, such as abstract classes, polymorphism, encapsulation, and so on.
Exercises:
- What is the syntax for creating a class in PHP?
- How can you define a constructor method in a PHP class?
- What is the difference between a static method and a non-static method in a PHP class?
- How can you create an object from a class in PHP?
- What is the purpose of the “extends” keyword in a PHP class?
Answers:
- The syntax for creating a class in PHP is “class ClassName {}”
- The constructor method in a PHP class can be defined using the __construct() function.
- A static method can be called without creating an object of the class, whereas a non-static method needs an object to be created and then it can be called.
- An object can be created from a class in PHP by using the “new” keyword, followed by the class name, and assigning it to a variable. e.g. $object = new ClassName;
- The “extends” keyword in a PHP class is used to indicate that a class is inherited from a parent class. It allows a class to inherit properties and methods from another class.