PHP OOP – Static Properties

PHP – Static Properties

Static properties can be called directly – without creating an instance of a class.

Static properties are declared with the static keyword:

Syntax

<?php
class ClassName {
  public static $staticProp = "iampsp";
}
?>

To access a static property use the class name, double colon (::), and the property name: Continue reading PHP OOP – Static Properties

PHP OOP – Traits

PHP – What are Traits?

PHP only supports single inheritance: a child class can inherit only from one single parent.

So, what if a class needs to inherit multiple behaviors? OOP traits solve this problem.

Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected). Continue reading PHP OOP – Traits

PHP OOP – Interfaces

PHP – What are Interfaces?

Interfaces allow you to specify what methods a class should implement.

Interfaces make it easy to use a variety of different classes in the same way. When one or more classes use the same interface, it is referred to as “polymorphism”. Continue reading PHP OOP – Interfaces

PHP OOP – Inheritance

PHP – What is Inheritance?

Inheritance in OOP = When a class derives from another class.

The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods.

An inherited class is defined by using the extends keyword. Continue reading PHP OOP – Inheritance

PHP OOP – Destructor

PHP – The __destruct Function

A destructor is called when the object is destructed or the script is stopped or exited.

If you create a __destruct() function, PHP will automatically call this function at the end of the script.

Notice that the destruct function starts with two underscores (__)! Continue reading PHP OOP – Destructor

PHP OOP – Constructor

PHP – The __construct Function

A constructor allows you to initialize an object’s properties upon creation of the object.

If you create a __construct() function, PHP will automatically call this function when you create an object from a class.

Notice that the construct function starts with two underscores (__)!

We see in the example below, that using a constructor saves us from calling the set_name() method which reduces the amount of code: Continue reading PHP OOP – Constructor

PHP OOP – Classes and Objects

A class is a template for objects, and an object is an instance of class.

OOP Case

Let’s assume we have a class named Fruit. A Fruit can have properties like name, color, weight, etc. We can define variables like $name, $color, and $weight to hold the values of these properties.

When the individual objects (apple, banana, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

Continue reading PHP OOP – Classes and Objects

PHP – What is OOP?

From PHP5, you can also write PHP code in an object-oriented style.

Object-Oriented programming is faster and easier to execute.

PHP What is OOP?

OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions. Continue reading PHP – What is OOP?