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 2 - OBJECT METHODS (AKA FUNCTIONS)
OOP PHP 2 - OBJECT METHODS (AKA FUNCTIONS)
Ok so if you get the bit about objects being similar to arrays in that they hold lists of data but are accessed through “properties” rather than “keys,” and you’re asking yourself: “that’s all?” then prepare yourself for the cool stuff. Here we go--lets lead with an example:
class Entry { public $title; public $pageCount; public function getInfo() { echo ‘The book, ‘ . $this>title . ‘ has ‘ . $this>pageCount . ‘ pages . '<br>'; } }
Now let’s loop through those objects previously created in the OOP PHP 1 tutorial:
foreach($articles as $article) { echo $article->getInfo(); }
The most important thing to notice--the practical goal accomplished here--is that in our “client code” we can echo the result of this code out to the browser:
echo ‘The book, ‘ . $this>title . ‘ has ‘ . $this>pageCount . ‘ pages . '<br>';
and do so without having to write that code. What I mean is that we wrote it one central place. If in another part of your web application, you need to echo this info again, well boom, just type echo $article->getInfo();. Capiche!
What has happened here is that we’ve given our object not just properties, but methods, and we did so with this line in the class code:
public function getInfo() { echo ‘The book, ‘ . $this>title . ‘ has ‘ . $this>pageCount . ‘ pages . '<br>'; }
In essence, objects derived from the Entry class have a function called getInfo() . To understand the difference between methods and the functions you previously learned about, let’s compare this to regular functions like date() or halveMonth(). Notice those do not come after $objectVariableName-> . Basically those sort of functions are global functions, where as the getInfo() function (called “method” in this case) applies only to a given object. What makes it special is that it innately has access to the other data stored in the object, specifically the data stored in its properties.
Now this is where you’re likely to have trouble understanding what I’m about to teach, but let’s give it a try. Go back to the code in your getInfo() method: echo ‘The book, ‘ . $this>title . ‘ has ‘ . $this>pageCount . ‘ pages';
See the “this” in bold. That’s a special php feature to refer to the current object. Remember, this method exists within a class, and a class is a template. It’s not the real thing. It’s not a real object. It’s a blueprint of what a real object will look like. So it needs a way to refer to itself. Later in client code objects generated from this class (technically called “instantiated”) will not use “$this” but rather the $variable name you assigned the object too.
Really, the best way to understand this is straightup by testing these examples and making a few modifications yourself, i.e. actually doing a little coding. It’s extremely simple to do, but hard to first get in its abstract written form. When you see it in action, it will instantly make sense--or at least a lot quicker. My suggestion is to flip to the Setting up Your Server tutorial, and get your server working and executing php, and immediately try these out by copy/pasting the code into one file. Typically, if reading a book about this, you’d go on to read many many more chapters all about the theory of OOP and all its features. But that’s stupid. If you can truly grasp this, all the rest will be fluff that just adds few other cherries on top.
Here’s the complete code snippet to copy/paste and execute:
class Entry { public $title; public $pageCount; public function getInfo() { echo ‘The book, ‘ . $this>title . ‘ has ‘ . $this>pageCount . ‘ pages . '<br>'; } } $article1 = new Entry; $article1->title = ‘The Bible’; $article1->pageCount = 100; $article2 = new Entry; $article2->title = ‘OOP PHP Programming’; $article2->pageCount = 200; $articles = array(); $articles[ ] = $article1; $articles[ ] = $article2; foreach($articles as $article) { echo $article->getInfo(); }
I’m not going to get into the other features of OOP, because in reality what you just learned here taught you the features of OOP php you’ll be using 80% of the time. Coders reading this may be freaking out, but the reality is you need to get your startup executed asap. You need to cut the bullshit and get productive asap. So this approach is very practical. Understand this, then refer to my further reading section to get a list of other books you should read to acquire true mastery.
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
SMM 3 - FORMULA TO FIND INFLUENCERS