This tutorial will be explaining the basics of Object Oriented Programming (OOP) classes. Let’s start with getting straight the following question:
What will be explained in this tutorial?
This tutorial will only explain the basics of OOP programming in PHP which consist of:
Why OOP?
First of all OOP can be, especially when creating huge scripts/projects, much easier to work with ( edit, modify, etc. ), it’s much more organized. Beside that it also makes use of classes, objects and functions which can make the programming a lot more efficient and fast. Also it offers a lot of (extra) features.
OOP – what’s it?
OOP is based on using objects created by classes. These classes can contain properties ( constants, variables ) and methods ( functions ). Let’s start with the classes of OOP and how to create them.
Classes & Objects
As mentioned above OOP uses classes which create the final ‘object’. To create a class we use the keywords class. This is how we create a basic class:
class MyBasicClass {
//... what should this class be cappable of - do?
}
You see the class we create starts with the keyword class followed by the name of the class we create. Then we put everything of the class between { and }. What we’re going to put here is what this class should be cappable of – what should it be able to do? We could for example make it show a welcome message which however is rather useless but just to give an idea of how it works. To do so, we need to create a function inside the class. Let’s call the function ’sayHello’.
class MyBasicClass {
//... what should this class be cappable of - do?
// say hello!
public function sayHello() {
echo "Hello! Welcome to my website!";
}
}
When it comes to creating functions or properties for a class, you have the choice of making it either a public , protected or private function or property. We made this function a public function so we can access it outside the class ( call it from an object ).
Public
Public functions or properties are accessable inside the class and parent classes but also outside the class (via/by object).
Protected
Protected functions or properties are only accessable inside the class and parent classes and not outside the class itself.
Private
Private functions or properties are only accessable inside the class itself and not by parent classes or objects.
Now we have created the class, but haven’t created the object, an instance of the class, yet. To do this we create a variable object and use the new keyword to create an instance of the class, the object.
$myObject = new MyBasicClass();
Now we created an instance of the class ‘MyBasicClass’ as an object ‘$myObject’. Do remember to put the creation of the class above this so an instance can be created of it. Otherwise it won’t find the class initialized and return an error.
Remember we made the function ’sayHello’ as a public function. So we should be able to access it outside the class using the object (instance of the class) we just created in $myObject. To do this we use the symbol ->.
$myObject = new MyBasicClass(); $myObject->sayHello(); //say hello to the world!
This makes it call the public function ’sayHello’. Let’s put it together so you can see how the real script looks like:
<php
class MyBasicClass {
//... what should this class be cappable of - do?
// say hello!
public function sayHello() {
echo "Hello! Welcome to my website!";
}
}
$myObject = new MyBasicClass();
$myObject->sayHello(); //say hello to the world!
?>
This will output:
Hello! Welcome to my website!
Note: However in most cases classes are created within a seperated file ( usually called something like: classname.class.php ) and included anywhere above the part where it creates the object. Usually just in the top/header.
Creating a variable inside the class can be done in exactly the same way as creating a function. We can again make it either public, protected or private.
Example:
class MyBasicClass {
//... what should this class be cappable of - do?
// say hello!
private $message = "Hello! Welcome to my website!";
public function sayHello() {
//show the message set in $message
}
}
We here created a private variable ‘$message’ which so will only be accessable inside the class, which is the only way we’ll need to use it. Now we want the function sayHello to show the message we just set. Therefore we’ll need to use the variables of the function, to be specific: the variable called ‘message’ ( which we just created ). Accessing variables of the same class inside the class can be done like this:
$this->variable_name
So in our case:
$this->message
It’s just the way to access variables of its own class. So let’s use this to show the welcome message inside the function ’sayHello’:
class MyBasicClass {
//... what should this class be cappable of - do?
// say hello!
private $message = "Hello! Welcome to my website!";
public function sayHello() {
echo $this->message;
}
}
This will output exactly the same as the previous class function if we create an instance object of it again and call for (execute) the function again. But we won’t do this again, it’s just the same way as done above ( creating an instance object of the class, then calling the function sayHello ).
We might want to be able to set the value of the welcome message (variable ‘message’) outside of the class, when creating an instance object of the class. So we could have multiple instances with each showing different welcome messages for example. We’ll use the default __construct function to do this, which will require the user to set variables for the class when creating the object (instance).
public function __construct($variable1, $variable2, $optionalVariable3 = NULL) {
}
It obviously needs to be a public function as it needs to be accessed automaticly when creating a new instance of the class. The variables that need to be given a value to is put between the brackets. In this example there’s one optional variable ( $optionalVariable3 ) which has been given a default value ( NULL ). So for that variable no other value is REQUIRED to be given, but optional. However in our case we only need to construct ONE variable, the variable containing the welcome message: $message.
public function __construct($message) {
}
However the value of $message ( set by the user when creating an instance object of the class ) is now only accessable inside this __construct function. To make it a public variable which is accessable all over the class, we will again use $this->variable_name to create the variable for the whole class:
public function __construct($message) {
$this->message = $message; //set the public variable 'message' equal to the value of $message
//as public variables can be accessed inside the whole class atleast
}
Let’s put it inside our previous class. So replace the $this->message variable with this construct function which sets it equal to the value given by the user when creating an object instance of the class.
class MyBasicClass {
//... what should this class be cappable of - do?
// say hello!
public function __construct($message) {
$this->message = $message; //set the public variable 'message' equal to the value of $message
//as public variables can be accessed inside the whole class atleast
}
public function sayHello() {
echo $this->message;
}
}
Now we could create an object, instance of the class and set the welcome message to show:
$myObject = new MyBasicClass("Example welcome message!");
And again use the -> symbol to call the function that shows the welcome message ( function ’sayHello’ ):
$myObject = new MyBasicClass("Example welcome message!");
$myObject->sayHello();
Let’s put it all together:
<?php
class MyBasicClass {
//... what should this class be cappable of - do?
// say hello!
public function __construct($message) {
$this->message = $message; //set the public variable 'message' equal to the value of $message
//as public variables can be accessed inside the whole class atleast
}
public function sayHello() {
echo $this->message;
}
}
$myObject = new MyBasicClass("Example welcome message!");
$myObject->sayHello();
?>
Instead of using the __construct function, the variable could as well be set as an argrument of the ’sayHello’ function itself. In that case you would need to set the welcome message when calling the function ( $myObject->sayHello(“welcome message”) ) instead of when creating the object.
Conclusion
So now we’re able to create a class, create (public, protected and private) functions and properties, construct a class so it’s constructable/modifieable/configurable outside the class (when creating the object), create an instance of the class and call functions and properties of the class using the object (instance of the class).
What’s next?
Play arround with creating objects, classes, methods and properties and try to think of useful and efficient ways to use them. If you don’t really have a clue yet on the use of them ( examples ), no worries: more tutorials will follow with useful examples of the use of classes and a tutorial further continueing on extending classes and creating more advanced classes as well, soon. An example of a very good use of classes would be to create a template system like Smorty e.g. which requires functions to set template to load, module to load, macros, etc., so classes would come very handy here. A tutorial on creating a basic template system will be written and posted as well for who’s interested.


Fri, Feb 12, 2010
PHP & MySql