Programming

PHP classes, objects, methods

Absolutely any object from our life we can describe by its characteristics and state, as well as influence this state. For example, your car has a certain color, make, engine, etc. Besides, it can drive, stand, accelerate or decelerate, it can be started, or you can turn on the headlights, for example. This is how the object-oriented approach works in programming; it describes some entity (object) in this case a car, using properties (color, make, engine, etc.). It also describes the interaction with the state of the object using methods (start the car, turn on the headlights, etc.).

Object-oriented programming is a rather complex subject in PHP. This makes it difficult for new programmers to understand PHP OOP, especially if they don’t have much programming experience.

Object-oriented programming appeared in PHP 4, but it gained momentum with PHP 5. In version 5, the PHP object model was rewritten to improve features and performance, and to introduce a complete object model for php classes.

What is object-oriented programming?
Object-oriented programming is a style of programming based on the concept of objects. It helps developers, prevents reinventing the wheel, and makes code easy to maintain and scale.

At first, object-oriented programming seems incomprehensible, but when you dig deeper, you’ll realize that it’s designed to simplify programming.

Why use object-oriented programming?
There are various advantages to using PHP OOP over procedural programming in PHP, such as:

Code reuse and rework: let’s say you want to build a Mercedes car object, and your friends want to build an Audi car object. You build your objects, but you discover that both objects have a lot in common between Mercedes and Audi, like the number of tires. In fact, both are cars. So instead of creating the two objects separately from scratch, if you have one class of cars with common properties and methods, you can save a lot of repetition by reusing and reworking the code.

Modularity: if you create several separate classes for different entities, you make them modular, and if you want to fix something, you only need to edit that particular class, not the others. This modularity turns out to be useful in troubleshooting.

Scalability: Object-oriented programs are more scalable than structured programs. The object interface provides all the information needed to replace an object, making it easy to replace old code or add new code to scale in the future.

Maintenance: programs are improved regularly with many changes. An object-oriented program is much easier to modify and maintain than an undirected program.

The concept of classes and objects
What is a class and object in PHP OOP?
Before we delve into the world of object-oriented programming, let’s learn about PHP classes and objects, which are the foundation of the PHP OOP paradigm.

A class is a data type defined by the programmer that contains various data types as well as various functions.

An object is an instance of a class. The object gets a copy of all the data and functions in the class, so it acts as an independent entity.

What are properties and methods?
Properties in a class are variables that can store data of different data types. A method is a function defined within a class.

All objects created in that class get a copy of that variable property and a copy of all member functions known as methods.

How do we create a class and an object?
In a class, we group the code that handles a certain topic. Thus, we must use a single noun like Car to name the class, which will handle logic about cars.

The syntax to create a class is very simple and straightforward. You must use the keyword class to declare the class followed by the name of the class.

  • Declare a class using the keyword class.
  • Use a singular noun and use the first letter.
  • If the class name contains more than one word, then capitalize each word using the so-called CamelCase style spelling. For example: JapaneseCar, AmericanCar.
  • A class instance is an object created from an existing class.
  • new is used to create an object from a class.

Now you can very easily create an object from a class using the keyword new.

How to define properties in a class?
Properties or member variables are used to add and store data in a class. Properties can store values of data types – strings, integers, decimals, logical values, etc. They are just like regular variables, but they are associated with an object instance, and thus you can only access a variable with an object.

Constructor and destructor
There are several methods known as magic methods in PHP. Among them the two most important magic methods are __construct and __destruct. These two methods create a constructor and a destructor for the class. Remember, magic methods always start with two underscores.

What is a constructor and a destructor?

The __construct method is a method that is called by the class when it creates an object. It is used to prepare a new object for use. Constructors can be used to assign parameter values to object properties.

The __destruct method is a function that is called by the class when the object is destroyed. It is usually used to clear memory when an object is destroyed.

Visibility Assignment in PHP
Earlier we discussed the use of one of the keywords public, private and protected when defining a property or method. Public, Protected and Private are the three types of visibility in PHP for controlling access to properties, variables or methods.

Public: a public method or variable is available everywhere.
Private: if a variable property or method is private, then it is only accessible within the class. For example: if the color property of the Car class is private, then you cannot access it outside the class like – echo $bmw->color; but a method like getColor can access it because it is inside the class.
Protected: if a property variable or method is protected, it can be accessed from inside the class or from any class that inherits from that class.
Why shouldn’t we use public visibility all the time?

Because it is not all that safe to keep everything publicly visible. To make it safe, PHP OOP introduced these different features.

Static properties and methods
A static method or property can be accessed without having to create an instance object. Simply use the class name, the scope resolution statement, and the property or method name.

We’ve already studied three modifiers: private, protected, and public. Static is the fourth access modifier in PHP OOP that allows you to access properties and methods without having to create objects from classes.

Conclusion
PHP OOP offers many advantages over procedural programming and provides an easier approach to building complex web applications with easier scalability.

You are now familiar with the style of object-oriented programming and should have a clear understanding of the basic concepts of PHP OOP, i.e. object-oriented programming.