We have a new website (We've moved)! So go check out Phosphorus.co

A Guide to OOP (Object Orientated Programming) in PHP Part 1

Posted in PHP, Tutorials
Preview Image

In this part we’ll cover the basics of classes, and how to put together your own class. We’ll also go over how to make variables.

Classes add a lot of functionality to your code and help to stop you repeating yourself over and over again as you might in conventional code. With classes and functions you’ll be able to make generic scripts that you can simply call when you need them.

Not only this but classes make your coding a lot more tidy, so you can usually find everything you’re looking for. When you know how to use them they can really speed up the speed at which you’re working.





Why should I learn OOP in PHP?

Well, sort of like real languages (sort of) once you know one programming language, its sort of similar across the board. Sure you’ll have the odd difference here and there, but there won’t be any really huge differences. Apart from that, PHP is one of the most widely used server side languages on the web, and it’ll definitely be worth your time learning to use objects in it.

Class is in Session

In Object Orientated Programming in PHP, we make classes. Classes are sets of functions that we can call when we need to, to use in our applications or websites. A class is pretty easy to make, just write class followed by the name you want to call that class. Then open (curly) brackets and close them at the end of the class. Here’s an example.

<?php

class name_goes_here {

}

?>

And there you have it, your first class. Are you proud of yourself? You should be! Lets add a function. These work the same way regular PHP funtions work. Just stick it inside the curly brackets.

<?php

class firstClass {

      function myFunction($variable, $variable_2 = 1) {

            if($variable = 1 && $variable_2 = 1) {
                  echo"Hello World";
            }

      }

}

?>

Don’t understand functions? Let me explain. Functions are declared by writing the word function, followed by the name you wish to call it and two curly brackets. Inside the curly brackets you can add variables inside these brackets, which help you add additional settings to that function.

For instance, I could have a variable in the curly brackets, called “$variable”. It would be required that I define this variable when calling the function (we’ll get to that later). We can then use that variable inside the function, so that the variable can serve a different purpose in different cases. We can also set the variable to a default value which it will take if the user doesn’t define it, so we can put “$variable = 1″ in the brackets where 1 is the default value. If a default value is given, then you don’t have to define that variable when calling the function. Here’s an example:

<?php

function myFunction($variable, $variable_2 = 1) {

     if($variable = 1 && $variable_2 = 1) {
           echo"Hello World";
     }

}

?>

In the above $variable and $variable_2 must be set as 1 for the script to output “Hello World”. $variable_2 is already set as 1, so the user just has to set $variable as 1 for it to work (when calling the function).

So lets say, somewhere else in your file hierarchy you need to call that class and function. First we have to include the class file, and call a new instance of the class. Then we will call our function (myFunction) and define the variable $variable as 1 so that we’ll get the “Hello World” text.

<?php

include"class.php";

$class = new firstClass();

$class->myFunction($variable = 1);

?>

Basically, that’s all there is to it, if you just want to create basic classes and functions. You can have as many functions as you like, so this will work fine:

<?php

class myClass {

      function functionOne() {
      }
      function functionTwo() {
      }
      function functionThree() {
      }

}
?>

Just call the functions in the same manner as we did before.

Variables variables variables variables

Classes can have variables. In PHP5 to define a variable in a class you would do something like this:

<?php

class myClass {

      public $var;
      public $house = "312";

      function myFunction() {

      }

}

?>

There are actually three ways you can declare a variable. You can change the public keyword to private or protected. In PHP4 you would have used the keyword var, which is now deprecated. However if you use it in PHP5 it will be as if you used public. You can use these keywords with functions as well, for example:

<?php

class myClass {

      public myFunction() {
      }

}
?>

By default however, all functions in a class are defined as public, so you do not need to write public beside every function.

But what do these words mean?

Public is basically the default mode, and will allow you to do everything we’ve explained so far. It makes no difference to your coding if you’re using just public.
Private means that the function or variable can only be accessed from inside that class (we’ll get to calling stuff inside a class next). This means that if you have a private function, you won’t be able to do this as it is outside the class it originated from:

<?php

include"class.php";

$class = new firstClass();

$class->myFunction($variable = 1);

?>

It also means (when we get to it) you can’t use private functions or variables in a child class. Public also allows the outside world to access/alter it, whereas private does not.
Protected is sort of a hybrid mode, in that if you have a protected variable it will act as public inside the class and that classes child class (if any), but will be private outside of that.

Calling stuff inside a Class

This is pretty basic stuff. We’ve learned how to create variables, classes and functions so far, but how do we actually use that stuff together? You may have noticed that when we included the class and declared a function we used a new operator which looked like an arrow (->). We use this to call stuff inside a class too. Lets say we have an empty variable at the top of our class and we need to call it inside a function in that class. We use a special variable called $this followed by our new arrow operator and the variable name. Look, here’s an example:

<?php

class myClass {

      public $variable;

      function myFunction() {
            $this->variable = "Hello There";
            print $this->variable;
      }

}

?>

The same thing will work with functions; for instance, if you have two functions and you want to call one inside another, just do this:

<?php

class myClass {

      function functionOne() {
            echo"Hello World";
      }
      function functionTwo() {
            $this->functionOne();
            echo" my name is Johnny";
      }
}

?>

Instead of trying to overload you with information, I’ve decided to do this as a tutorial series, in different parts so you don’t get too much information at once. You should be able to make your own simple classes with this information.

Anyway, mess around with the information I’ve given you so far. Hopefully you should be able to make your own little creations. Until next time, my friend.

Click Here for Part 2


Share This

 

 
 

Author: Johnny




Johnny is the owner of Webtint and usually the sole contributer to the site (apart from the ocassional guest post).


Comments



designfollow November 28, 2009 at 6:50 pm

thank for this info


Adii December 3, 2009 at 7:04 pm

Good tut !!! Thnx for postinh :)
waiting for more awesome :):)


Nazmul Hossain December 12, 2009 at 5:47 pm

Great.I am new in PHP .I have one question to u.Why we use “private and protected”.we can not access them outside of the class. How i use a variable outside of a class if it declare as private or protected ???


    Jonathan December 12, 2009 at 6:07 pm

    You may find in a particular situation that private and protected are beneficial. However if you want to access the variable or function from outside the class (or the classes child) you’re going to have to used public instead.


      Nazmul Hossain December 13, 2009 at 6:13 am

      Thanks…………………
      u have any good example or website name where i can found private,public and protected variable or function are use together.

      Thanks for reply


Nazmul Hossain December 14, 2009 at 1:06 pm

Thanks a lot………….
Now i understand.Thanks again…………..


Robin January 19, 2010 at 9:51 pm

Awesom tutorial!

This is the first tutorial that made it to my brains :). Even the ones in my own language didn’t made it as clear as this tut!

Regards


shaffy August 12, 2010 at 7:46 am

thank for this info


Jonathan December 13, 2009 at 2:14 pm

Okay, I can’t find a good full example for you, but think of it like this.

Lets say you’re building an API or something along those lines, and you don’t want the users of it accessing certain functions or variables because they contain data you don’t want the public to see (MySQL Data maybe?). Therefore you could say that private and protected are used for security reasons in some cases.

On top of this though, using private and protected can let yourself know where these variables and functions are going to be used, thereby helping you to organize your code in some form.



uberVU - social comments




Leave us a Comment



Put your name here:

Your email goes here:

Got a Website? Put it here (optional):

And finally, your comment:




Sponsors