Lesson 1
PHP Classes and Object-Oriented Programming Basics
Lesson Overview

Welcome! Today, we're exploring PHP classes, a fundamental aspect of Object-Oriented Programming (OOP) in PHP. Using practical examples, we'll delve into the essential concepts of PHP classes, including their structure, attributes, and methods.

PHP Classes Refresher

Let's kick off with a look at PHP classes. Crucial in OOP, PHP classes encapsulate related data and functions within a compact unit called an object. Think of a video game character as an instance of a class, with specific attributes (like health or strength) and methods (such as attack or defense).

A PHP class acts as a blueprint containing attributes and methods. Attributes represent data pertinent to a class instance, while methods are functions or actions that manipulate this data. Each class comes with a constructor, which is responsible for initializing class attributes.

In PHP, the $this keyword is essential for accessing the class instance's attributes and methods. When a new class instance is instantiated, $this allows the object to maintain its state and behaviors.

php
1<?php 2 3class GameCharacter { 4 // Attributes 5 public $name; 6 public $health; 7 public $strength; 8 9 // Constructor 10 public function __construct($name, $health, $strength) { 11 $this->name = $name; 12 $this->health = $health; 13 $this->strength = $strength; 14 } 15 16 // Method 17 public function attack($otherCharacter) { 18 $otherCharacter->health -= $this->strength; 19 } 20} 21 22?>

Note: we will cover constructors in the next unit of this course, but in the meantime, consider them just as methods that construct the instance of your class given certain input parameters!

Class Attributes

Attributes in PHP classes store data associated with each instance. In our GameCharacter class, name, health, and strength are such attributes. You can access a class attribute with an instance of the class, followed by an arrow (->), and the attribute name.

Attributes are initialized within the constructor. PHP uses the $this keyword to refer to the current object instance and set attribute values.

php
1<?php 2 3class GameCharacter { 4 // Attributes 5 public $name; 6 public $health; 7 public $strength; 8 9 // Constructor 10 public function __construct($name, $health, $strength) { 11 $this->name = $name; 12 $this->health = $health; 13 $this->strength = $strength; 14 } 15} 16 17$character = new GameCharacter("Hero", 100, 20); // instance of the class 18echo $character->name . "\n"; // prints: Hero 19echo $character->health . "\n"; // prints: 100 20echo $character->strength . "\n"; // prints: 20 21 22?>

Here, the constructor initializes the class attributes with provided arguments, differentiating one class instance from another and maintaining the instance's state.

Class Methods

PHP classes also contain methods — functions that manipulate class data. For instance, the attack method in the GameCharacter class simulates an attack by one character on another.

php
1<?php 2 3class GameCharacter { 4 // Attributes 5 public $name; 6 public $health; 7 public $strength; 8 9 // Constructor 10 public function __construct($name, $health, $strength) { 11 $this->name = $name; 12 $this->health = $health; 13 $this->strength = $strength; 14 } 15 16 // Method 17 public function attack($otherCharacter) { 18 $otherCharacter->health -= $this->strength; 19 } 20} 21 22$character1 = new GameCharacter("Hero", 100, 20); // First instance 23$character2 = new GameCharacter("Villain", 80, 15); // Second instance 24 25echo $character2->health . "\n"; // prints: 80 26$character1->attack($character2); // character1 attacks character2 27echo $character2->health . "\n"; // prints: 60, health decreased after the attack 28 29?>
Examples of PHP Classes, Attributes, and Methods

To further our understanding of PHP classes, let's build a BankAccount class. This will help us model real-world entities using OOP by defining attributes like an account holder's name and balance alongside methods for depositing and withdrawing money.

php
1<?php 2 3class BankAccount { 4 // Attributes 5 public $holderName; 6 public $balance; 7 8 // Constructor with a default balance of 0 9 public function __construct($holderName, $balance = 0) { 10 $this->holderName = $holderName; 11 $this->balance = $balance; 12 } 13 14 // Method to deposit money 15 public function deposit($amount) { 16 if ($amount > 0) { 17 $this->balance += $amount; 18 echo "$amount deposited. New balance: $this->balance\n"; 19 } else { 20 echo "Deposit amount must be positive.\n"; 21 } 22 } 23 24 // Method to withdraw money 25 public function withdraw($amount) { 26 if ($amount > 0 && $amount <= $this->balance) { 27 $this->balance -= $amount; 28 echo "$amount withdrawn. Remaining balance: $this->balance\n"; 29 } else { 30 echo "Insufficient balance for the withdrawal or amount is not positive.\n"; 31 } 32 } 33} 34 35$account = new BankAccount("Alex", 1000); // An account with an initial balance of 1000 36 37// Perform some transactions 38$account->deposit(500); 39// Deposit money, prints: 500 deposited. New balance: 1500 40 41$account->withdraw(200); 42// Withdraw money, prints: 200 withdrawn. Remaining balance: 1300 43echo "Final balance in {$account->holderName}'s account: {$account->balance}\n"; 44// prints: Final balance in Alex's account: 1300 45 46?>

This example further highlights how classes encapsulate data (attributes) and functionalities (methods), enabling the emulation of real-world scenarios. The BankAccount class facilitates the creation of objects representing bank accounts, showcasing the powerful organizational benefits of using classes in PHP.

Lesson Summary

Excellent work exploring PHP classes, their attributes, and methods. PHP classes help you organize your code, enhancing its readability and manageability. Now, challenge your understanding with practice exercises to reinforce your refreshed knowledge. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.