Did you know FaceySpacey is the creator of Redux-First Router and Universal?

The Startup Incubator.
Your one stop social media shop
Resource Categories:

Technical
Non-Technical
Refer A Friend
Popular Articles
DEVELOPMENT TOOLS 1 - VERSION CONTROL (Mercurial)
CONTRACTING 2 - TOP 10 EASY SITE CREATOR TOOLS
Subscribe
-
Technical
-
PHP
-
OOP PHP 1 - INTRO TO OBJECT ORIENTED PROGRAMMING IN PHP OOP PHP 2 - OBJECT METHODS (AKA FUNCTIONS) OOP PHP 3 - INHERITANCE IN OOP PHP 1 - Introduction To Programming PHP 2 - DYNAMIC CODE PATHS & VARIABLES PHP 3 - FUNCTIONS PHP 4 - SCOPE PHP 5 - HOW TO LEARN PROGRAMMING PHP 6 - ARRAYS PHP 7 - LOOPS PHP 8 - CONCLUSION & MORE LEARNING TECHNIQUES
- OOP PHP 1 - INTRO TO OBJECT ORIENTED PROGRAMMING IN PHP
OOP PHP 1 - INTRO TO OBJECT ORIENTED PROGRAMMING IN PHP
We’re going to start by going directly to a code example that will highlight the power of object oriented programming. I wish I saw this as the first thing I learned when learning OOP. Usually books/tutorials on OOP explain all the theory and individual features of an OOP language, but you’re left having no clue on the magic you can do with it. Once you see and understand the magic, neurons in your brain will get to work figuring out what aspects of OOP you need to learn next to get productive, and it will quickly all make sense. Here we go:
$article1 = new Entry; $article1->title = ‘The Bible’; $article1->pageCount = 100; $article2 = new Entry; $article2->title = ‘OOP PHP Programming’; $article2->pageCount = 200; $articles = array(); //this is the creation of an array $articles[ ] = $article1; $articles[ ] = $article2; foreach($articles as $article) { echo ‘The book, ‘ . $article->title . ‘ has ‘ . $article->pageCount . ‘ pages. <br>'; }
When output to the browser from your web server, the result is:
Ok so what’s happening? The first thing I want to point out is that I used an array. I used an array to store multiple objects. The 2 objects were created with these lines:
$article1 = new Entry; $article2 = new Entry;
I used an array to store 2 objects. Then I looped through the array, echoing values from the two objects. I was able to access the data stored in the object in identical ways, which is why the foreach loop worked. The idea is that the object is a package of structured data. It’s like a variable in that it starts with a $ symbol, i.e. $article1. However it contains more data, similar to how an array contains a list of data. Syntactically it’s different in that you access the sub-data like this: $article1->title; . I.e. the “keys” are strings (never numbers) that come after -> .
Next the idea is that each of the 2 objects is generated from the same template, specifically with the “new Entry” statement. There’s a term you will hear non-stop as you dive deeper into OOP for this template--it’s called a “class.” For now just think of the Entry class as a template, from which we can generate similar objects. Elsewhere this template/class is defined, similar to how a function is defined. The main thing here is that each objects generated from this template/class all have the same “keys,” i.e. again the part that comes after ->. In objects, these “keys” are actually called “properties”--so that’s how we’ll refer to them from here on out, now that you get the correlation.
In this example, the “Entry” class has 2 properties: “title” and “pageCount”. And each object generated from this class/template can assign their own unique values to these properties. This allows for us to iterate through an array containing these 2 objects and request data from these 2 properties and know that something will be there. Imagine you had 10 objects stored in an array, and each object corresponded to a blog article, i.e. it carried with it all the content necessary to display a blog article. So it would have the title, body of the article, author name, list of tags, etc. That’s where the power lies. You can write very simple code that will extract data from a similar place in a list of many objects like we did here:
foreach($articles as $article) { echo ‘The book, ‘ . $article->title . ‘ has ‘ . $article->pageCount . ‘ pages. <br>'; }
capiche. Because the data is structured in a similar format, we can be sure it will always be there.
Right now, you should be asking yourself what the fundamental differences are between arrays and objects. Both contain basically lists of data, right? Well, technically you could achieve similar results with arrays, but you’d have to individually define the keys of each array to be things like “title” and “pageCount”. With object oriented programming, you can define a class once like this:
class Entry { public $title; public $pageCount; }
You do that once, and you can create objects created from that template/class any time you want without writing the same code. Note: to more experienced OOP developers, don’t worry I’ll soon start referring to classes as templates--I just feel that correlation is extremely important to make for newbies, so I’ll keep combining the two with a slash for a little while longer before just calling them “classes” always.
So the key principle here is that each of these objects have the same “interface.” Interfaces are the key to good programming. If you’re new to this, which most of you are reading this, then the term “interface” is sure to get you thinking, curious, and possibly confused. My favorite analogy here is that of a Microwave. You know how it has all the buttons to control it, and the numbers 1-9 to determine how long you want your food to cook for, etc, right? And you can cook your meal with these buttons without having to know the internals of the microwave, i.e. you don’t have to know the slightest thing about how micro “waves” vibrate your food to cook it, etc. Well, that’s an interface. An interface provides a simple way to control something without having to know anything about its internals.
In object oriented programming, these object $variables are accessed by these interfaces, i.e. the properties “title” and “pageCount”. The takeaway here is that the interface is CONSISTENT!!! From object to object (generated from the Entry class), the interface is the same. And that’s what allows you to loop through objects (such as those in an array) and echo out the unique data within each object, but without having to code any different code to access each object. They are all accessed the same.
Related Entries
- OOP PHP 1 - INTRO TO OBJECT ORIENTED PROGRAMMING IN PHP
- OOP PHP 2 - OBJECT METHODS (AKA FUNCTIONS)
- OOP PHP 3 - INHERITANCE IN OOP
- PHP 1 - Introduction To Programming
- PHP 2 - DYNAMIC CODE PATHS & VARIABLES
- PHP 3 - FUNCTIONS
- PHP 4 - SCOPE
- PHP 5 - HOW TO LEARN PROGRAMMING
- PHP 6 - ARRAYS
- PHP 7 - LOOPS
- PHP 8 - CONCLUSION & MORE LEARNING TECHNIQUES
Comments

SMM 3 - FORMULA TO FIND INFLUENCERS