You are on page 1of 3

CHAPTER 3

Functions, Classes,
and Traits
So far, you have been using PHP for simple top-down scripting.
In this chapter, you will learn how to declare and use classes and
functions (including class definitions, visibility, inheritance, and traits).
Also, object-oriented programming (OOP) will be introduced and
explained.
The real power of PHP comes with the ability to declare and use
classes and functions. As a quick overview, classes are (as you saw in the
last chapter) definitions for objects to use when being created. The class
definitions then turn into objects that you can use to store and manipulate
data. Functions are reserved words within PHP that you declare, define,
and call in order to do small or complex tasks. The reason you separate out
these tasks into functions is so that you can abstract them and their usage.
Abstract is a fancy way of allowing for a function to be called by multiple
sources for one purpose. Instead of writing a function in multiple classes to
do the exact same thing, you can set that function apart in a class and call
it whenever you need. The duplication of code is one thing to look out for
when refactoring or even working through your logic.
With these concepts in mind, this chapter will focus on the world of
OOP, which models the application and development around real-world
objects such as users, cars, colors, or even vegetables.

© Gunnard Engebreth, Satej Kumar Sahu 2023 33


G. Engebreth and S. K. Sahu, PHP 8 Basics, https://doi.org/10.1007/978-1-4842-8082-9_3
Chapter 3 Functions, Classes, and Traits

This chapter will cover the following topics:

• Object-oriented programming (OOP)

• Class definitions

• Class visibility

• Class inheritance

• Polymorphism and abstract classes

OOP
The three basic concepts in object-oriented programming are

• Encapsulation: This is concerned with the optics or


exposure of information between classes and the rest of
the application. The benefits of this include

• Complexity reduction: Data that is not needed


outside of certain classes is not available outside of
those classes.

• Data protection: Allowing access to data


through GET/SET methods creates a flexible and
maintainable codebase.

• Polymorphism: The ability to have one form of data


structure with multiple uses and implementations

• Class extension and abstraction allow for this.

• Inheritance: Classes have the ability to share


information between each other depending on
visibility and the parent/child relationship.

These concepts are more than just a specific method of declaring


methods and properties. This is about thinking, structure, data flow, and

34
Chapter 3 Functions, Classes, and Traits

coding methodology. It’s comparable to the difference between knowing


the rules of chess and knowing what a London opening is and how to
defend yourself. These concepts drive how you think about data, usage,
and manipulation. While object-oriented programming can be taught, it
is best learned through hands-on development. OOP might seem rigid
and forceful at first, but there will come a time where the freedom and
creativity that come from defining, extending, and abstracting make sense
and begins to drive your development process.

Reviewing Class Definitions


Let’s review the class definition from above:

<code>
<?php
   class UserClass
{
      /* User variables */
      var $firstName;
      var $LastName;

      /* Member functions */


      function setFirstName($firstName){
         $this->firstName = $firstName;
      }

      function getFirstName(){
         echo $this->firstName;
      }

      function setLastName($lastName){
         $this->lastName = $lastName;
      }

35

You might also like