Let’s talk about OOP is not. When you look at different plugins in WordPress, you’ll see a common pattern of class blueprint with all static properties and methods. That is not OOP. Why? You can’t create an object from statics. Statics don’t know anything about objects, as they belong to the class. This episode will show you why this matters. The design approach of wrapping static functions and properties within a class is called Class Wrapper. It’s a design pattern that utilizes the class’ ability to hide complexity. When used for that purpose, it can add helper functionality to your […]
Labs are hands-on coding projects that you build along with Tonya as she explains the code, concepts, and thought processes behind it. You can use the labs to further your code knowledge or to use right in your projects. Each lab ties into the Docx to ensure you have the information you need.
Each lab is designed to further your understanding and mastery of code. You learn more about how to think about its construction, quality, maintainability, programmatic and logical thought, and problem-solving. While you may be building a specific thing, Tonya presents the why of it to make it adaptable far beyond that specific implementation, thereby giving you the means to make it your own, in any context.
In PHP, a class can have static properties and methods. A static is bound to the class blueprint and not the object or any of the objects. Therefore, all of the objects share the static code. If the class has a static property, then any object that changes that property is reflected and changed for every single object of that class. This is a very important episode. Make sure you watch it at least once. This episode will help you to cement the different of statics and objects. You access the static with a double colon ::. Within the class, […]
Internal Control of Object Creation
When you create a new object, i.e. instantiate it with the keyword new, PHP gives you a constructor. You can use this constructor to control what happens when the object is created. Why would you want to do that? An object should be created in a known starting state. This approach makes sure the object is ready to use. You pass in the starting information and then let the object initialize itself. You can let it setup and set the properties as well as run the initialization processes. The other reason is that the implementation, i.e. the external application code […]
Working within the Object with $this
This episode covers a hard topic for most OOP newbies to grasp: the concept of accessing an object’s internal properties and methods using the PHP reserved pseudo-variable $this. Each object uses the class blueprint as its code. Within the object itself, you reference that object’s properties and methods using $this. For example, let’s say you wanted to check the first name on a User object. You would do: $this->first_name within the object itself. Look at that code. Internally within the object, the variable $this refers to the object that you are working with. It’s just that one object and not […]
Hiding Away the Complexity
Part of OOP is the ability to hide away complexity from the outside world. The object controls the sequence for the work to be done. It only exposes what it is needed for the application to work with the object. For example, let’s relate this difficult concept to a car. When you start your car, the car handles all the tasks to start the car. It handles the air and gas mixtures, pistons, spark plugs, and circuits. You don’t have to manually sequence through each of those tasks yourself to start that car. No, you just push the button or […]
Putting the Object to Work
It’s time to put your objects to work. It starts by thinking about the work (behavior) that a user would do. How is your code going to use and work with the object? Let’s talk through the different behaviors, such as checking if the user is logged in, changing password, etc. Then let’s make an object work by calling one of the methods. Remember, that in OOP, the keyword function is referred to as a “method” (confusing I know). Then let’s stop for a moment and see if you are grasping the concept of objects. Notice that you created two […]
Creating an Object
The class is the blueprint. Then how do we make an object from it? You use the new keyword. Think about that. If you wanted to build a house, the architect gives the blueprints to the craftsmen. They will build you a new house. The house is the object, built off the blueprint. Got it?! Do you see how new makes sense? In this episode, you will create your first object from the blueprint you built in the last episode. You are creating a new user. New terms that you will learn are: Instance – meaning the object, i.e. it’s […]
Objects have characteristics, which in PHP are called properties on the class blueprint. Let’s think about what a user would have, such as first name, last name, user id, etc. Then you’ll add these to your blueprint. Next, you’ll create two different users and set each one’s properties. As you are doing this episode, think about how each user is separate. Each one is an instance of the blueprint. But they are separate users, i.e. separate objects. Each has a name. Each has an ID. They are separate. You’ll be introduced to visibility keyword public and the object arrow accessor […]
Meet the Class Blueprint
The first step is to define our blueprint. In PHP, the keyword class alerts PHP that this is a blueprint. The syntax is: An object is a noun, i.e. a thing. Therefore, its name should be a singular noun. How you code the name varies depending upon if you are following PSR or the WordPress coding standard. Per PSR, class names should be in StudlyCaps, where the words are smooshed together and the first letter is capitalized. Per the WordPress Coding Standard, the words are separated by an underscore and the first letter of each word is capitalized. The curly […]
What is an object?
An object has both characteristics and behavior. It is built from a blueprint. To help you grasp the concept, let’s relate software objects to physical, real world objects. We are going to talk about homes in a new subdivision. You’ll see how the concept of homes (objects) and be translated into software objects and OOP.