PHP Classes/Objects
Building blocks of Object-Oriented Programming
🎯 What are Classes and Objects?
Classes are blueprints for creating objects. An object is an instance of a class containing properties and methods. Classes help organize code and make it reusable and maintainable.
<?php
// Define a simple class
class Car {
public $color = "red";
public function drive() {
return "The car is driving!";
}
}
// Create an object
$myCar = new Car();
echo $myCar->color; // Output: red
echo $myCar->drive(); // Output: The car is driving!
?>
Output:
red
The car is driving!
Key Concepts
Class
A template defining properties and methods
class Person {
public $name;
}
Object
An instance created from a class
$person = new Person();
Properties
Variables that belong to a class
public $name = "John";
Methods
Functions that belong to a class
public function greet() {
return "Hello!";
}
🔹 Creating a Class
A class is defined using the
class
keyword followed by the class name and curly braces containing properties and methods.
<?php
class Book {
// Properties
public $title;
public $author;
public $pages;
// Method
public function getInfo() {
return "$this->title by $this->author ($this->pages pages)";
}
}
?>
🔹 Creating Objects
Objects are created using the
new
keyword. You can create multiple objects from the same class, each with different property values.
<?php
class Dog {
public $name;
public $breed;
public function bark() {
return "$this->name says Woof!";
}
}
// Create first object
$dog1 = new Dog();
$dog1->name = "Max";
$dog1->breed = "Labrador";
// Create second object
$dog2 = new Dog();
$dog2->name = "Bella";
$dog2->breed = "Poodle";
echo $dog1->bark(); // Output: Max says Woof!
echo $dog2->bark(); // Output: Bella says Woof!
?>
Output:
Max says Woof!
Bella says Woof!
🔹 Accessing Properties and Methods
Use the arrow operator
->
to access object properties and methods. Inside class methods, use
$this
to refer to the current object.
<?php
class Student {
public $name;
public $grade;
public function study($subject) {
return "$this->name is studying $subject";
}
public function getGrade() {
return "$this->name's grade: $this->grade";
}
}
$student = new Student();
$student->name = "Alice";
$student->grade = "A";
echo $student->study("Math"); // Output: Alice is studying Math
echo $student->getGrade(); // Output: Alice's grade: A
?>
Output:
Alice is studying Math
Alice's grade: A
🔹 Complete Example
Here's a practical example combining everything we've learned about classes and objects.
<?php
class BankAccount {
public $accountNumber;
public $balance;
public $owner;
public function deposit($amount) {
$this->balance += $amount;
return "Deposited $$amount. New balance: $$this->balance";
}
public function withdraw($amount) {
if ($amount <= $this->balance) {
$this->balance -= $amount;
return "Withdrew $$amount. New balance: $$this->balance";
}
return "Insufficient funds!";
}
public function getBalance() {
return "Account balance: $$this->balance";
}
}
// Create account
$account = new BankAccount();
$account->accountNumber = "12345";
$account->owner = "John Doe";
$account->balance = 1000;
echo $account->deposit(500); // Output: Deposited $500. New balance: $1500
echo $account->withdraw(200); // Output: Withdrew $200. New balance: $1300
echo $account->getBalance(); // Output: Account balance: $1300
?>
Output:
Deposited $500. New balance: $1500
Withdrew $200. New balance: $1300
Account balance: $1300