Featured, PHP, Programming, Web Development
PHP Object-Oriented Programming for Beginners
· Linet M.
Object-oriented programming (OOP) scares many PHP developers at first. The syntax looks unfamiliar. But OOP is just a style of grouping related actions and data into classes, which produces code that is more compact and far easier to maintain.
OOP exists to stop you repeating yourself. Imagine you have a function that sends emails. Later you need to add a BCC recipient to every email. If that logic lives in one class, you change it once. If it is scattered across a dozen files, you hunt down every copy. That is the practical case for OOP.
Classes and Objects
A class is the blueprint. An object is the thing you build from it.
Think of a User class as the definition: every user has a username and a password, and every user can login. An object is a specific user (say, john with password 01juan01). John is one instance of the User class. You can create hundreds of instances from the same class.
Creating a class in PHP:
class User {
}
Creating an object from that class:
$john = new User();
Adding Properties
A property is a variable that belongs to an object. Add them inside the class body:
class User {
public $username;
public $password;
}
public means the property is accessible from anywhere, inside the class and outside it. You write and read properties through the object using the arrow operator:
$john = new User();
$john->username = 'john';
$john->password = '01juan01';
echo $john->username; // john
The value lives inside $john, not inside the class itself. That is why User->username makes no sense: a class has no stored value, only the instance does.
Adding Methods
A method is a function defined inside a class. Add a greeting method:
class User {
public $username;
public $password;
public function greet() {
echo 'Hello, ' . $this->username . '!';
}
}
$this refers to the object that called the method. When $john calls greet(), $this->username resolves to john.
Calling the method:
$john = new User();
$john->username = 'john';
$john->greet(); // Hello, john!
With multiple objects:
$users = [];
$users[] = new User();
$users[] = new User();
$users[0]->username = 'john';
$users[1]->username = 'manolo';
foreach ($users as $user) {
$user->greet();
}
// Hello, john!
// Hello, manolo!
Magic Methods
PHP provides special methods that run automatically when certain events happen to an object. Their names start with a double underscore.
Constructor and Destructor
__construct() runs when you create an object with new. Use it to set default values or populate the object from a database record:
class User {
public $username;
public $password;
public function __construct() {
$this->password = '1234';
}
}
$user = new User();
echo $user->password; // 1234
Pass data into the constructor to pre-fill the object at creation time:
class User {
public $username;
public $password;
public function __construct($username) {
$this->password = '1234';
$this->username = $username;
}
}
$user = new User('john');
echo $user->username; // john
__destruct() runs when the object is destroyed, either when the script ends or when you call unset() on it. Typical uses include closing database connections and freeing resources:
class User {
public $username;
public $password;
public function __construct($username) {
$this->password = '1234';
$this->username = $username;
}
public function __destruct() {
echo 'User destroyed.';
}
}
$user = new User('john');
unset($user); // User destroyed.
PHP has more magic methods beyond these two. The official documentation covers all of them.
Class Inheritance
A class can inherit properties and methods from another class. This is where OOP pays off most clearly.
Say User is the parent class. You want an Admin class that does everything a User does, plus the ability to ban other users. Use extends:
class User {
public $username;
public $password;
public function greet() {
echo 'Hello, ' . $this->username;
}
}
class Admin extends User {
public function ban($user) {
echo $user->username . ' has been banned.';
}
}
$user = new User();
$user->username = 'john';
$admin = new Admin();
$admin->username = 'superlopez';
$admin->greet(); // Hello, superlopez
$admin->ban($user); // john has been banned.
Admin inherited username, password, and greet() from User and added a new ban() method without touching the original class.
Overriding Methods
To change a parent method in the child class, redefine it:
class Admin extends User {
public function greet() {
echo 'Hello, admin >> ' . $this->username;
}
public function ban($user) {
echo $user->username . ' has been banned.';
}
}
$admin = new Admin();
$admin->username = 'superlopez';
$admin->greet(); // Hello, admin >> superlopez
To keep part of the parent's behavior, call it explicitly with parent:::
class Admin extends User {
public function greet() {
parent::greet();
echo '!';
}
public function ban($user) {
echo $user->username . ' has been banned.';
}
}
$admin = new Admin();
$admin->username = 'superlopez';
$admin->greet(); // Hello, superlopez!
Visibility: public, protected, and private
Visibility controls which code can read or write a property or method.
public: accessible from anywhere, inside or outside the class.
protected: accessible only from the class itself and any class that extends it. External code gets an error if it tries to reach a protected member.
private: accessible only from the class that defines it. Even child classes cannot see a private member:
class User {
public $username;
private $password;
}
$user = new User();
$user->password = 'secret'; // Fatal error: Cannot access private property
Use private for sensitive fields like passwords and internal state that no outside code should touch directly.
Static Methods and Properties
A static method or property belongs to the class itself, not to any particular instance. You do not need to call new to use it.
Static properties also hold their value across all instances for the lifetime of the script. A common use is a counter:
class User {
public $username;
public $password;
public static $count = 0;
public function __construct() {
self::$count++;
}
}
$user1 = new User();
echo User::$count; // 1
$user2 = new User();
echo User::$count; // 2
Inside a static method, use self:: instead of $this. $this refers to an instance, but static members have no instance attached to them.
OOP in PHP follows the same principles you find in Java, Python, and every other class-based language. Once the syntax clicks, you write less code, break it less often, and share classes across projects without copy-paste. If you're working through a PHP or web development assignment and need a hand, PHP Assignment Help connects you with developers who build production PHP day-to-day.
For a broader look at how these object-oriented patterns compare across languages, Inheritance vs Composition: OOP Tradeoffs breaks down when to use each approach. And if you want to see OOP applied to a real PHP project, 13 PHP Tips That Make Development Faster covers practical patterns that show these concepts in action.
Related articles
- Featured
Programming Project Ideas to Build and Earn
10 programming project ideas for students and developers: web apps, social networks, computer vision, robotics, and open source contribution.
Oct 15, 2014
- PHP
PHP vs JSP vs ASP: Why PHP Wins
A direct comparison of PHP, JSP, and ASP as server-side languages, covering syntax, cost, flexibility, and database support to explain why PHP stays the default choice.
Jan 31, 2018
- PHP
7 Popular PHP Frameworks Compared
Compare 7 PHP frameworks (Laravel, CodeIgniter, CakePHP, Zend, Yii, Symfony, Phalcon) on architecture, performance, and best use cases.
Aug 26, 2016


