PHP OOP Introduction
In this page:
class ClassName {
public $property;
function method_name() {
// uses $this->property
}
}
$object = new ClassName();
$object->method_name();
What is Object-Oriented Programming?
Object-oriented programming organizes code around objects that bundle data (properties) and behavior (methods) together, instead of scattering related variables and functions loosely across a script.
Example: What is Object-Oriented Programming?
<?php
// Define the class `Car`
class Car {
// Declare the public property `$color`, set to "red"
public $color = "red";
// Define the function `drive` with no parameters
function drive() {
// Print "Driving a $this->color car" to the output
echo "Driving a $this->color car";
}
}
// Create a new `Car` instance, stored in `$car`
$car = new Car();
$car->drive();
?>
Login to try C/C++/Java/PHP code in the editor
Benefits of OOP
A class is a blueprint that defines what properties and methods its objects will have, while an object is one concrete instance created from that blueprint — much like a house blueprint versus an actual house built from it.
Example: Benefits of OOP
<?php
// Define the class `Car`
class Car {
// Declare the public property `$color`
public $color;
}
// Create a new `Car` instance, stored in `$myCar`
$myCar = new Car();
$myCar->color = "blue";
// Print `$myCar->color` to the output
echo $myCar->color;
?>
Login to try C/C++/Java/PHP code in the editor
Classes vs Objects
PHP has supported OOP since PHP 4 and expanded it heavily in PHP 5 and later, so modern PHP frameworks like Laravel and Symfony are built almost entirely around classes, objects, and the patterns that come with them.
Example: Classes vs Objects
<?php
// Define the class `Product`
class Product {
// Declare the public property `$name`
public $name;
}
// Create a new `Product` instance, stored in `$p`
$p = new Product();
$p->name = "Laptop";
// Print `$p->name` to the output
echo $p->name;
?>
Login to try C/C++/Java/PHP code in the editor
Properties in OOP
OOP encourages reusability and organization: once you define a Product class, you can create as many product objects as you need, each with its own data but sharing the same behavior defined once in the class.
Example: Properties in OOP
<?php
// Define the class `Product`
class Product {
// Declare the public property `$name`
public $name;
// Declare the public property `$price`
public $price;
}
// Create a new `Product` instance, stored in `$p1`
$p1 = new Product();
$p1->name = "Laptop";
// Create a new `Product` instance, stored in `$p2`
$p2 = new Product();
$p2->name = "Phone";
// Print `$p1->name . " / " . $p2->name` to the output
echo $p1->name . " / " . $p2->name;
?>
Login to try C/C++/Java/PHP code in the editor
Methods in OOP
The core OOP pillars — encapsulation, inheritance, polymorphism, and abstraction — each solve a different organizational problem, and later sections in this course build up each one individually with concrete PHP examples.
Example: Methods in OOP
<?php
// Define the class `Product`
class Product {
// Declare the public property `$name`, set to "Book"
public $name = "Book";
// Define the function `describe` with no parameters
function describe() {
// Print `"This product is: " . $this->name` to the output
echo "This product is: " . $this->name;
}
}
// Create a new `Product` instance, stored in `$p`
$p = new Product();
$p->describe();
?>
Login to try C/C++/Java/PHP code in the editor
- Confusing a class (the blueprint) with an object (an instance created with
new). - Forgetting
$this->when using a property inside a method, which creates an unrelated local variable. - Making every property public, which lets any code change the internal state.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: