PHP – The $this Keyword

The $this keyword refers to the current object, and is only available inside methods. Look at the following example: <?php class Fruit { public $name; } $apple = new Fruit(); ?> So, where can we change the value of the $name property? There are two ways: Inside the class (by adding a set_name() method and use $this): <?php class Fruit { public $name; function set_name($name) { $this->name = $name; } } $apple = new Fruit(); $apple->set_name("Apple"); echo $apple->name; ?> 2. Outside the class (by directly changing the property value): <?php class Fruit { public $name; } $apple = new Fruit();...

Object-Oriented PHP With Classes and Objects

In this article, we're going to explore the basics of object-oriented programming using PHP classes. We'll start with an introduction to classes and objects, and we'll discuss a couple of advanced concepts like inheritance and polymorphism in the latter half of this article. What Is Object-Oriented Programming (OOP)? Object-oriented programming, commonly referred to as OOP, is an approach which helps you to develop complex applications in a way that's easily maintainable and scalable over the long term. In the world of OOP  (to create object in PHP), real-world entities such as Person, Car, or Animal are treated as objects. In object-oriented programming, you interact...