Understanding ClassesIntroduction
This is an introductory tutorial to classes and OOP (Object Oriented Programming) in PHP; for those who know the basics of the PHP language.
Classes are really extraordinary and allow you code powerful programs if you use them well. This forum is generally based on classes and a group of functions
Skeleton of a class
Classes are really easy to use actually and nothing to be afraid of. I used to shy away from them before I started using them, but when I started to learn classes, I was suprised and I understood I overestimated it.
You can name a class with anything you want. The class names could be made with letters, numbers, and underlines "_". Remember, that if you use numbers in the class name, it
cannot be the first character, you need to have a letter be the first character. You tell the PHP parser that it is a class by putting the word 'class ' in front of the class name.
Below is an example of a simple small class:
<?php
class abc123_class {
var $var = false;
function func_test()
{
// Function test
}
}
?>
- <?php
- class abc123_class {
- var $var = false;
-
- function func_test()
- {
- // Function test
- }
- }
- ?>
Don't worry if you don't understand all of that right now, I would explain all of that later in this tutorial.
What is so good about a class, is that you could pass variables between functions without having to make them global or passing the variable into the argument of the function. It is also possible to share variables and function between classes, and I would talk about this later as it's big enough for a whole different tutorial
The variables within the class that could be passed between function to function that are inside of a class are accessible with the '
$this' keyword. It is recommended that you pre-define them before you use them to avoid getting errors like "such and such variable not defined", and to just create default values to those variables.
Below is an example class using the variable thing.
<?php
class example {
var $name = false;
var $last = false;
function name($first, $last)
{
$this->name = $first;
$this->last = $last;
}
function show()
{
echo $this->name . ' ' . $this->last;
}
}
?>
- <?php
- class example {
- var $name = false;
- var $last = false;
-
- function name($first, $last)
- {
- $this->name = $first;
- $this->last = $last;
- }
-
- function show()
- {
- echo $this->name . ' ' . $this->last;
- }
- }
- ?>
I used string concatenation there to show that you could do that with class-wide variables like you would with normal classes. Basically,they
are normal variables and could be used as such... as an array, boolean, intager, float, etc. etc., they just have a clas-wide scope and are accessible by the keyword '
$this'. Don't get that confused

The keyword '
$this' could only be used within a method that is inside of an object... in this case, inside of a function that is inside of a class. You can't have the keyword outside of a function and inside of a class as that gives you an error.
You can have as many variables, as many functions inside of a class as you want/need for your system/script, there is no limit.
Using Classes
To use an object/class, you need to create a new instance of the object and set it to a variable named however you like. Classes should be defined before they could be initiated. Below is an example of initiating a class:
<?php
$object_res = new example();
?>
- <?php
- $object_res = new example();
- ?>
Using the functions and variables inside of that class/object, isn't difficult, you use the arrow ( -> ) like you do to access class-wide variables with the keyword '
$this'. Below is an example:
<?php
$object_res = new example();
$object_res->name = 'Mark';
$object_res->last = 'Jones';
$object_res->show();
?>
- <?php
- $object_res = new example();
-
- $object_res->name = 'Mark';
- $object_res->last = 'Jones';
-
- $object_res->show();
- ?>
The above code example would print "Mark Jones" becuase the function
show() is defined to echo the first name and the last name.
There are many types of variables formally known as 'Data Types' and this one is 'object'. You can read more on datatypes
here to understand more on a few of them and to get a list of all data types.
A Few Warnings
If you are going to use classes after this tutorial and you don't know any more abou the classes, I wouldn't suggest naming any function the same as your class name. It is possible, and in most cases a good idea to do that, but I haven't covered that in this tutorial and will in a later tutorial.
What it's called though is a magic method and is equivalent to
__construct, but again, I would talk about all this at a later tutorial,so bare with me here
What you can do
What you can do to reinforce your learning experience is practice writing classes with variables talking between functions within a class. The first thing I done in a class is a database access wrapper... I had a function to connect to the database, and a function to select and that stuff... right now, the completed class could be found on
PHP-Classes repository.
Conclusion
Hopefully you learned the basics of classes and ready to go to the more advanced stuff once the tutorial comes out here.