Object oriented programming was a foreign concept when I first started with it. I grew up writing procedural code all the way back to when I was 6 and coding in BASIC on my Atari 400! Procedural code was the way, but no longer. I am all about OOP now that I’ve grasped the basics and have started creating code for some pretty complicated objects. And as I get more and more into coding, the complexity abounds — but OOP makes complexity fairly straightforward, organized and easy to work with.
A few basics first…
Objects in PHP and many other languages are stand-alone. Objects initialize their properties and have various methods that can do anything a procedural program can do. But by providing methods that do repetitive tasks over and over again, these methods end up saving you lines of code and tons of time because you’re not having to redo things, reverse engineer your procedures and delve through lines and lines of code. Plus objects are inherently logical, which is what I love about them. I can put an object down and come back to it in six months and put together what’s going on in no time!
- Properties. In PHP, properties are nothing more than variables or arrays. The only thing is those properties belong to a given class.
- Methods. Methods in PHP are functions. But again they belong to your class and must be called from within the instance of your class.
Let’s define an object…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class myObject { // Let's define our first property public $item; // And let's give it a method too function set_item($val) { $this->item = $val; } // And let's define one more method function print_item() { echo $this->item; } } |
And with that, you have defined the class myObject and given it a single property and defined two methods. So far things look pretty procedural, but if you loaded this into your PHP parser, nothing would happen. That’s because the object is defined, there’s no procedure defined on how to access the object!
Let’s say we save that code into a file called myObject.class.php. Now you’d want to access the object. To do that, you’d include the object then declare a new instance of the object. Once that’s done you can define a procedure for interaction with your new object. Like this…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // First, include the object's code (remember what I said, // nothing will happen here.) include('myObject.class.php'); // Now we instantiate the object: The object's properties and methods // are stored in the variable $obj like so... $obj = new myObject(); // Now we can interact with the object. Let's give the item property // a value... $obj->set_item(344); // Nothing happens...but now we can do something! $obj->print_item(); // outputs 344 |
And that’s it. You’ve defined an object and interacted with the object. Using object definitions allows you to store properties and methods in a file and access them with a simple include line. You can also instantiate multiple instances of an object to work over different sets of data the same way. It’s very handy and can be more efficient at some things than procedural programming.
You can also use objects to work with repetitive tasks, such as an email handler that receives four fields: to, from, subject and body. It could then generate and send emails to all the persons in the to line from the person listed in the from line.
Conclusion
This was a simple introduction to object oriented programming. It’s something that comes with practice and the more I’ve dealt with it the more I enjoy working with it.