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 – Abstract Classes

PHP – What are Abstract Classes and Methods?

Abstract classes and methods are when the parent class has a named method, but need its child class(es) to fill out the tasks.

An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared, but not implemented in the code. Continue reading PHP OOP – Abstract Classes

PHP OOP – Class Constants

PHP – Class Constants

Class constants can be useful if you need to define some constant data within a class.

A class constant is declared inside a class with the const keyword.

A constant cannot be changed once it is declared.

Class constants are case-sensitive. However, it is recommended to name the constants in all uppercase letters. Continue reading PHP OOP – Class Constants

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 – Access Modifiers

PHP – Access Modifiers

Properties and methods can have access modifiers which control where they can be accessed.

There are three access modifiers:

  • public – the property or method can be accessed from everywhere. This is default
  • protected – the property or method can be accessed within the class and by classes derived from that class
  • private – the property or method can ONLY be accessed within the class

Continue reading PHP OOP – Access Modifiers

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?

PHP Exceptions

What is an Exception?

An exception is an object that describes an error or unexpected behaviour of a PHP script.

Exceptions are thrown by many PHP functions and classes.

User defined functions and classes can also throw exceptions.

Exceptions are a good way to stop a function when it comes across data that it cannot use. Continue reading PHP Exceptions