This article explores the key concepts of classes and objects in PHP, including how to declare classes, instantiate objects, and define class properties and methods.
Declaring Classes
A class in PHP is a blueprint for creating objects. It defines properties (attributes) and methods (functions) that describe the behavior and characteristics of the objects created from the class. Declaring a class in PHP involves using the class keyword followed by the class name and a pair of curly braces that enclose the properties and methods.
Here is a simple example of declaring a class:
make = $make;
$this->model = $model;
$this->year = $year;
}
// Class method
public function displayInfo() {
return "This car is a {$this->year} {$this->make} {$this->model}.";
}
}
?>
In this example, the Car class has three properties ($make, $model, $year) and two methods: the constructor method __construct() and the displayInfo() method. The constructor method is a special method that is called automatically when an object is instantiated from the class. It initializes the properties of the object.
Instantiating Objects
To create an object from a class, you use the new keyword followed by the class name and parentheses. This process is known as instantiation. Once an object is created, you can access its properties and methods using the arrow operator (->).
Here’s how you can instantiate an object from the Car class and use its methods:
displayInfo(); // Outputs: This car is a 2020 Toyota Corolla.
?>
In this example, the Car class is included from an external file, and an object $car1 is created with specific values for its properties. The displayInfo() method is then called to display information about the car.
Class Properties and Methods
Class properties and methods are the building blocks of a class. Properties represent the state of an object, while methods define its behavior.
Class Properties
Class properties are variables that belong to a class. They can have different access levels: public, protected, or private. The public keyword means that the property can be accessed from anywhere, the protected keyword means that the property can be accessed within the class itself and by inheriting classes, and the private keyword means that the property can only be accessed within the class itself.
Here’s an example demonstrating different access levels of class properties:
name = $name;
$this->age = $age;
$this->salary = $salary;
}
public function getAge() {
return $this->age;
}
public function getSalary() {
return $this->salary;
}
}
$person = new Person("John Doe", 30, 50000);
echo $person->name; // Outputs: John Doe
echo $person->getAge(); // Outputs: 30
echo $person->getSalary(); // Outputs: 50000
// The following lines will cause errors because $age and $salary are not public
// echo $person->age; // Error
// echo $person->salary; // Error
?>
In this example, the Person class has three properties with different access levels. The name property is public, the age property is protected, and the salary property is private. Methods getAge() and getSalary() are provided to access the protected and private properties, respectively.
Class Methods
Class methods are functions that belong to a class. They can also have different access levels (public, protected, or private). Methods define the actions that objects of the class can perform.
Here’s an example demonstrating class methods:
multiply($a, $b);
}
}
$calc = new Calculator();
echo $calc->add(5, 3); // Outputs: 8
// The following lines will cause errors because subtract and multiply are not public
// echo $calc->subtract(5, 3); // Error
// echo $calc->multiply(5, 3); // Error
// Accessing the private method through a public method
echo $calc->getMultiplication(5, 3); // Outputs: 15
?>
In this example, the Calculator class has three methods with different access levels. The add() method is public, the subtract() method is protected, and the multiply() method is private. The getMultiplication() method provides access to the private multiply() method.
Static Properties and Methods
PHP also supports static properties and methods, which belong to the class itself rather than any particular object. Static properties and methods can be accessed without creating an instance of the class, using the scope resolution operator (::).
Here’s an example demonstrating static properties and methods:
In this example, the MathHelper class has a static property $pi and a static method square(). They are accessed using the class name followed by the scope resolution operator and the property or method name.
Conclusion
Classes and objects are fundamental concepts in PHP’s Object-Oriented Programming paradigm. By understanding how to declare classes, instantiate objects, and define class properties and methods, developers can create well-structured, modular, and reusable code. Whether you’re defining a class with public, protected, or private properties, or using static properties and methods, mastering these concepts will enhance your ability to build robust and scalable PHP applications.