Chapter 9: Object-Oriented Programming in PHP
In this chapter, we will explore object-oriented programming (OOP) in PHP. We will cover the principles of OOP, creating classes and objects, defining properties and methods, inheritance, and encapsulation.
9.1 Introduction to object-oriented programming:
Object-oriented programming is a programming paradigm that focuses on creating objects that encapsulate data and behavior.
OOP principles include encapsulation, inheritance, and polymorphism.
9.2 Creating classes and objects:
Classes are blueprints for creating objects, defining their properties and methods.
Objects are instances of classes, representing individual entities in the application.
Example:
php
""
<?php
// Class definition
class Car {
// Properties
public $brand;
public $color;
// Methods
public function startEngine() {
echo "Engine started!";
}
}
// Object instantiation
$myCar = new Car();
$myCar->brand = "Toyota";
$myCar->color = "Red";
// Accessing object properties and methods
echo $myCar->brand; // Output: Toyota
$myCar->startEngine(); // Output: Engine started!
?>
9.3 Defining properties and methods:
Properties represent the state or characteristics of an object.
Methods are functions defined within a class, representing the behavior or actions that objects can perform.
Example:
php
""
<?php
class Circle {
// Property
public $radius;
// Method to calculate area
public function calculateArea() {
return 3.14 * $this->radius * $this->radius;
}
}
// Object instantiation
$myCircle = new Circle();
$myCircle->radius = 5;
// Accessing object properties and methods
echo $myCircle->calculateArea(); // Output: 78.5
?>
9.4 Inheritance:
Inheritance allows classes to inherit properties and methods from other classes.
The child class extends the parent class, inheriting its features and can also add new properties and methods.
Example:
php
""
<?php
// Parent class
class Animal {
public function eat() {
echo "Eating...";
}
}
// Child class inheriting from Animal
class Dog extends Animal {
public function bark() {
echo "Woof!";
}
}
// Creating an instance of the child class
$myDog = new Dog();
// Accessing inherited method
$myDog->eat(); // Output: Eating...
// Accessing child class method
$myDog->bark(); // Output: Woof!
?>
9.5 Encapsulation:
Encapsulation refers to the bundling of data and methods within a class, hiding internal details from the outside world.
Access modifiers like public, protected, and private control the visibility and accessibility of properties and methods.
Example:
php
""
<?php
class Person {
// Public property
public $name;
// Private property
private $age;
// Public method
public function greet() {
echo "Hello, my name is " . $this->name;
}
// Private method
private function getAge() {
return $this->age;
}
}
$person = new Person();
$person->name = "John";
$person->greet(); // Output: Hello, my name is John
?>
By following the explanations and examples in this chapter, you will gain a solid understanding of object-oriented programming in PHP, allowing you to create classes and objects, define properties and methods, implement inheritance, and utilize encapsulation to build modular and maintainable applications.
0 comments:
Post a Comment