Let me explain, before I start, that this might be a little off. It's mostly community and feedback created, so yeah. I don't use OOP a lot, and am kind of new to it. The original tutorial was shit, but I've added a lot to it from feedback and articles. Credits are listed at the bottom of the page.
OOP is mainly used for variable reusage and, as many people say, relates to real life situations so it's easier to understand.
PHP is a multiple-paradigm language, meaning you can write it in either procedural or object orientated. The language itself is not fully object orientated, and you cannot make it so.
So, in OOP, we usually have two files: the class, and then the handler. Let's get started with our class:
class.php
Code:
<?php
class OOP {
public $variable;
}
?>
As you can see, the class name does not have to be the same as the file name (but it is highly recommended to be so), like how it is in java and many other programming languages. Also, notice how we defined our variable as a public status. This is because if you do not put anything before the variable, it gives you an error. Please note that there are a bunch of things you can put in front of your variable - it all really depends to be honest. They are called modifiers and modify how the variable is used. For some of the modifiers, please look at the bottom of the page. You may have also noticed that our variable is undefined. It can be defined or kept this way, it does not matter.
Now, we need to define our variables. Well, if we did not define them straight away, then how do we do this? It's simple, and here's how:
Code:
<?php
class OOP {
public $variable;
function __construct() {
$this->variable = "Hello";
}
}
?>
As you can see, we defined it using the $this-> method. Now, let me explain what $this-> means:
In any programming language, "this" references to the class you are working in. If you attempt to do a "this" in a regular PHP file with no class, you will get an error as there is no object/class to reference the variable to. Also, we defined it in a function and not just out in the open. Why? Because we would have gotten a syntax error (explained below).
* QUICK STOP NOTE *
- Syntax Error: You would have received the "EXPECTING T_FUNCTION" error when using any function besides your own in a class
- public function __contruct {} - This is a constructor (listed at the bottom of the page). A constructor is called whenever a object initializes as a result from a class.
Now that we have defined our variable, how would we display it on the page? Well, we certainly wouldn't go straight out and echo it through the class. You can do this, but you would need a whole function, and then you would need to display the function. This isn't as neat, and personally, I like to have a handler:
handler.php
Code:
<?php
$OOP = new OOP();
?>
Note: Your object should be named after your class. Read the next paragraph for information on objects!
Now, as you can see, we are creating a new object named $OOP (objects are the result of initiating a class) and running it through our handler. If you run this, however, you will get an error. For all of you Java Guru's, this is not like Java anymore, and let me explain:
In PHP, since classes do not have to be named after the file name, you must include the class first. In Java, however, all classes must be named after their filename. This is why in java you do not need to include the file, but only define it.
So, to include our file (for those of you who don't know this), we would do this:
[PHP]<?php
require_once('class.php'); # once to prevent errors
$OOP = new OOP();
?>[/PHP]
Now it can fetch the class correctly.
So, to echo our variable, we would then do this:
Code:
echo $OOP->variable; # Calls the variable $variable from the object $OOP (class: OOP)
And Voila, you are done!
Notes/References
- You cannot echo or do anything of the sort in a class, you can only define variables and functions
- OOP Can be very confusing to some people, so if you do not get this, I advise you to go read up on it more
Definitions
- Class: This is a programmer-defined datatype, which includes local functions as well as local data. You can think of a class as a template for making many instances of the same kind (or class) of object.
- Object: An individual instance of the data structure defined by a class. You define a class once and then make many objects that belong to it. Objects are also known as instance.
- Constructor: Refers to a special type of function which will be called automatically whenever there is an object formation from a class.
Modifiers
- Public: Accessible by everything and anything
- Protected: Accessible only in the same class or classes that extend the class it is in
- Private: Accessible ONLY in the class it is declared in
Credits
- Object Oriented Programming in PHP - A good-looking tutorial for OOP users. I got most of the definitions here, but none of my notes were from here. I haven't read it, so feedback would be appreciated
- Eight : Really helped me with some additions and suggestions. Shoutout to him.