Tutorial: PHP and OOP, a beginners guide
This is a tutorial covers the major concepts of OOP in PHP5 with usable examples that actually do something (however useless the something may be). The tutorial at this time is incomplete, only covering a few fundamental subjects, all subjects are complete in and of themselves. I will consistently add pieces when I have the time to.
What is OOP?Object Oriented Programming, referred to as OOP is a way to efficiently reuse data in an easy to read way. It is not necessarily the best way in sheer lines of code or processing time, it also does not belong in small scripts. It makes the script easier to read, maintain and expand, its simple functions are just a fancy, easier naming scheme, but its advanced features give you incredible power.
PHP4 vs. PHP5This tutorial and the examples within will not work with any PHP version below 5, I recommend you use the latest stable version of PHP available. So if you haven't upgraded since the stone age, I recommend you do it soon. Virtually all shared hosts come with PHP5 unless you specifically requested otherwise, if you are on a PHP4 server, you can almost always request a transfer.
PHP4 has rather limited OOP features, limited to the point where I do not use OOP in any PHP4 code. PHP5 offers OOP capabilities comparable to computer programming languages such as C++, with these full features OOP becomes a key component in large scripts. Full OOP support was by far the largest and most needed upgrade between versions. Summed up, use PHP5.
OOP BasicsThis will be a long section, one of the reasons OOP is so hard is because there is so much to grasp where it wont make sense till you know it all. I will do my best to explain things in a logical manor, if things do not make sense please bear with me.
The most common implementation of OOP that I use is with a user system, sometimes its the only object in the entire script because its the only piece that is being reused. Lets say you are making a user system for a client, all you need to do is authorize it to see if they are logged in and are or arent admin. There are two ways you can go about it, the first is the non OOP way (I actually use the OOP code and the non is an adaption of it).
Note to C++ Users:To avoid the confusion that got me for a while, the -> is not a bitwise shift, it is the equivalent of a period. This may sound stupid but it had me confused for ages.
Wait, PHP doesn't need creation of a variable to use it, if it finds an undefined variable it creates it so it can be used, right? This is not the case with classes, with classes you have to do it like in languages like C++ or Java, you have to declare every variable , don't do that and you will get an error.
Now for the $this-> statement, $this-> simply means it is the variable in the instance that is being run. I will explain instances in the next section.
Implementation of Classes
Now that you know how to declare and put variables in a class, how do you use it? To explain instances, lets say there is a photo you want to edit, you don't make the edits to the original, you make them to the copies, or instances. Classes work sort of the same way, classes cant be directly edited, but you can make copies of them and work with those. You can make as many copies (instances) of a class as you please. So if you have a user class and two implementations (such as as a game of pong), you don't have to go through the many lines of declaring another one, you just create two instances. To make an instance of a class, you simply use this code after the declaration.
Lets create at a useless script that is far too small to need OOP and doesn't really do anything but show the above lessons. This script will have a number variable and a function to add and subtract it.
ConstructorsConstructors are statements that tell the class what to do when initialized. Generally, these set variables to values automatically instead of manually and individually later on down the road, but they can be used to do anything a normal function can be.
You can even edit other instances of other classes so long as they are created before the class you are currently making is made. I do not recommend this because it takes one of the key features away from OOP, portability, making the class reliant on another piece of code like that makes it harder to move.
Like normal functions, construct can hold parameters, meaning if you want to force setting certain variables but you know that the value may not be consistent, just use this.
Inheritance is not PHP passing a variable to another application when the die() command is called. Inheritance is a method of taking variables from an already created class to another one. It is called with the extend command. When you inherit another classes properties, designated variables become part of the extending class. Lets look at what it does
The next is echo $this->first_class_variable;. When you extend a class, all scoped variables in the parent class are brought down as if you typed them all in the current class. Therefore you refer to all inherited variables as $this->, not parent::.
Why do you need to refer to functions with parent::? $this-> finds that particular instances copy of the variable, class_name:: or parent:: (reference to the extended class) don't look for instances, it looks for the value of the member in the class itself. This only works on functions, this will not work with variables. Functions don't need to be copied for each instance because their commands are static, what you typed them to be is what they are, bar none. Variables on the other hand are dynamic and can be changed, each instance has to have a separate copy of it. Therefore when referring to a variable you use $this-> because it has been created in that instance and class::function() for functions because they remain apart from instances.
Parent simply tells PHP that the class name is whatever its parent it, you can just as well call the classes name out directly
Scopes tell PHP who can see that variable. There are three scopes that can be applied
Public: Anyone can access it, outside, inside and child classes.
Example: Any above code, anything can access it.
Protected: Only functions within that class and child classes can access it
Private: Only functions within that class can access it.
Inheritance's Practical UsesI must admit, I rarely use inheritance. Inheritance's best use is for functions that will be used many times, by many different classes. Especially if they all must do the same thing and the process is open to revision. Therefore when you modify one, they all will respond to it. This enhances one of the main purposes of OOP, reusable code.
Note:When using var to declare a variable in a class, it sets it to public, this was only continued from php4 (which didn't have scopes) for code compatibility reasons. It is deprecated and probably shouldn't be used, but it doesn't make a difference unless you need to use private or protected. Functions left without scope definition are public, I generally don't define public in my code, it doesn't make a difference.
I intend on making additions whenever I have time to write them. If you find any typos or mistakes please do alert me to them and I will revise them as soon as possible.
-Cleaned the code by using public instead of var for class definitions. Var and public serve the same purpose, but var is not proper php5, it is only supported for php4 compatibility. Description after code has been altered accordingly.
-Made the tutorial more visually appealing.
Feel free to post this anywhere as long as the below line is here
Originally written by Village Idiot
|All times are GMT. The time now is 03:21 AM.|
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0