By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

PHP: classes vs functions and variables

This article on OOP discusses some of the benefits of using OOP rather than just functions, variables and includes.

Edited: 2016-01-04 06:11

In the beginning it may not make much sense why you use classes instead of just functions and includes in PHP, but once you get some experience working with them you will likely start to love the extra structure and semantics it adds to your code.

One actual reason why we use classes is to avoid conflicts with other code. If we only used includes in our PHP applications, then you will likely sooner or later have conflicting code such as overlapping function and variable names. This is something we avoid with classes, because it allows us to encapsulate the functions and variables in classes and objects.

Do not allow this to confuse you. There is nothing technical to the term object. Think of an object like a real-world object that performs certain functions, such as a car or an airplane. Objects are made from classes, and allows us to interact with the properties and methods of the class.

Many get the properties and methods mixed up, and find it confusing to know which is which. It may be easier to remember if you think of the variables in the objects or classes as "properties", and the functions, as something that can be done a certain way, hence it makes sense that functions are also called "methods".

Class inheritance

When you have multiple classes, you will typically arrange them in a way that makes sense for your application, think of your application as an object such as a car or an airplane, where the different functions corresponds with the different parts of the object, engine and wings.

Arranging your classes is not always easy or intuitive, and sometimes you just have to make a choice and stick with it. But anyway, the arrangement often have a main class which is then extended with a second class, which can be extended with a third class. Etc. Often you will have just one class handling database queries and connections, which is then extended.

The resulting source code ends up being very structured and easy to maintain, you usually know exactly where to look for which functions. If you had just had everything in one big file, then you would often search, or struggle to remember line numbers. This is why we split code into multiple files and classes. Often you only have one class per PHP file to keep things easy to find.

When you extend a class, it inherits all of the properties (variables) and methods (functions) from it's parent class, and the functions of the child also becomes available inside the parent. Hence the saying, the class has been extended.

Encapsulation

The code is kept encapsulated in objects, and as such it will not interfere with other code. Some times your application will just have one big object put together from multiple smaller classes. At other times it will have multiple objects – this is especially true if you are using PHP liabries and other code found on the internet.

When having multiple objects rather than a bunch of PHP includes with bare functions, the encapsulation in objects ensures that the functions and variables do not overlap.

It is also possible to declare properties and methods as private, making them accessible from other methods (functions) in the class. This is useful if you want to restrict access to certain variables from the outside, to prevent that they are accidentally incorrectly used. Accessing properties or methods from the outside of a class could easily end up breaking an application, and sometimes it just clutters the code.