Learning PHP: Part 4: Objects

Learning PHP

PHP: Hypertext Preprocessor (PHP) is a server-side scripting language frequently used as part of web development and as a command-line tool for common tasks.


Like many programming languages, PHP supports the object-oriented programming model. In PHP, like with thinking in this model in other languages, large problems and complex tasks are broken into objects and their relationships.

In the object-oriented programming paradigm, there are two types of relationships between objects: is-a and has-a. If an object contains another object, it “has” it. If an object inherits (“extends” in PHP terms) from another, it “is” that object.

PHP has Class

Screenshot 2018-10-30 15.09.30

In PHP, object are defined using the class keyword. Like in many other programming languages, a class encapsulates its contents. From the opening to its closing curly bracket, everything within them is “in” the class and part of the object.

Variables defined outside of a class work according to their scope. When a variable is defined within a function, it cannot be accessed outside of it. Variables created within a PHP are usually in the global scope because of this. When working with objects, an additional rule can come into play.

Public and Private

Screenshot 2018-10-30 15.16.42

PHP understands the practice of private and public variables and functions. When using the keyword private, a variable or function will only be usable within that class. It is private to that object.

Such distinctions are helpful when thinking through objects and their relationships. Some objects have data that only it uses while others have combinations of data that can be used outside of it of through functions it provides.


Screenshot 2018-10-30 15.17.28

In object-oriented programming, a special type of function constructs the object. This allows for passing data into an object to help initialize certain values or connect it to other objects upon creation. In PHP, this is a special function named __construct().

When used within a class in PHP, the __construct() function is called with a new object is created. Acting as a function, data can be passed into it as parameters when used when initializing the object.

This is Me

Screenshot 2018-10-30 15.23.20

PHP supports use of the this keyword to differentiate a variable inside of a class from one outside of it. Because variables start with the dollar sign, so does this in PHP. Written as $this, the keyword allows PHP to reference variables and functions within itself.

Objects Points to Their Variables and Functions

Screenshot 2018-10-30 15.25.26

When working with object in PHP, its variables and functions are referenced using an arrow, “->”. This both visually shows that something is part of an object and is used to “point” out from it.

When used with the $this keyword in PHP, objects can “point” to their own variables and functions, referencing them within themselves. Used outside of an class, the same arrow is used to reference something inside of a class used outside of it.

Extending (Inheritance)

While variables inside of an object are an example of a has-a relationship, is-a patterns can also be used. In PHP, this pattern uses the keyword extends.

Screenshot 2018-10-30 15.28.15

When one class extends another in PHP, it inherits its variables and functions. This means that the original class is a parent of this new object. In fact, that’s how PHP thinks of it. To use the functions of the parent in the child class, the keyword parent is used.

Within the child class, it can call its parent’s functions. To do this in a constructor, for example, it would be parent::__construct(). This allows for using the parent’s constructor to set values. Because the child has the same variables and functions, this can be a quick way to design a more generic class and the differences of more specific ones in its children.

Creating New Objects

Screenshot 2018-10-30 15.35.00

In PHP, the new keyword is used to create new objects. When working with a constructor, some initial values can be passed into the object as parameters to that function. Its returned value is the new object.

Screenshot 2018-10-30 15.37.11

Once created, the variables and functions of the object can be used through “pointing” to them.

Play with the example on Repl.it!