[PHP] The Basics: Classes

Post all your tuts or request for tuts here.
Post Reply
Baseball435
Posts: 548
Joined: Sun May 30, 2010 3:49 am

[PHP] The Basics: Classes

Post by Baseball435 »

Hey everyone, this is my first tutorial in the php section and im going to be talking to you about classes and functions in php, also known as OOP (object oriented programming).

So first im going to explain what a class and function is.

A class is a blueprint to create instances of class. So practically it is a big container full of different functions which you can use anywhere. If you make the classes in an external file (what we will be doing) then you can call the class and then functions over and over instead of having to type in the same thing 50 times.

A function is a command which can do several different things of your choosing like inserting a username and password into a database to doing calculations. Today im going to be showing you calculations and displaying text using classes and functions.

So lets get started! :D

The first thing we are going to do is make the class page. So lets just name it:

ourclass.php

Code: Select all


<?php

class ourclass {


}

$ourclass = new ourclass();

?>

So that should be what your ourclass.php file should look like right now. Practically we are making a class by typing class in front and then giving it a name: ourclass. Then after that you put a open and then close curley brace.

At the bottom you see the variable called: $ourclass. What we are doing here is we are making a variable name and then setting it to the class. Then after that we declare new and then we type the name of the class which in this case is: ourclass(). If the name was hisclass then you would put new hisclass(); Also the variable name can be whatever you want, like HallsIsAmazing, as long as the word after new is the name of the class.

Then later on we can include this php file and use the $ourclass variable to call functions.

Now after this we are going to add onto it. So we are going to make a class that displays "IndieResource.com FTW". So now make the code look like this in your ourclass.php file:

ourclass.php

Code: Select all


<?php

class ourclass {

         function sayThis()
         {
                 $return = "IndieResource.com FTW";
                 return $return;
         }

}

$ourclass = new ourclass();

?>

So now we make a function, by typing function in front, and then putting a name. In this case we put: sayThis(); After a function name you ALWAYS put () after it. If not, you will get errors. Then we open it and close it with curely braces and inside we put what we want the function to do. So in this case it is going to make a variable called $return and then make it equal to "IndieResource.com FTW". Then at the end we type return $return.

So what this return thing does is it takes a variable or anything that you want and sends it back to whatever called the function. So if i called this function from a different page I would put $say = $ourclass->$sayThis(); Then it will save whatever the return is to the variable $say. But I will explain it more in a second so don't worry about this right now.

Now we are going to make our main php file. We are going to call it index.php and just copy what I put into the code below..

index.php

Code: Select all


<?php

include("ourclass.php");

$say = $ourclass->sayThis();
echo $say;

?>

Now let me explain this code: First off we include our ourclass.php file so that we can use all of the variables and functions that are inside of it. Then we declare a variable called $say and set it equal to $ourclass->sayThis(); What this does is first it uses the variable, $ourclass, from the ourclass.php file and then we put a funny little arrow -> like that. This is how you call all of the functions that you have in your class. So if we had a function called IndieResource we would do $ourclass->IndieResource(); just like that. So then after the -> we put the function, sayThis();

After that line we echo out the function $say. So what the return does in the ourclass.php is it takes the $return and send its to the variable called $say. Then we can display $say by echoing it out.

If you have done everything correctly then it should say: IndieResource.com FTW on your screen. If now then post your problems below. I will be making another tutorial on calculations later. Hope this helps!

~baseball435
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: [PHP] The Basics: Classes

Post by Xaleph »

Nice tutorial, kind of the hands-on edition behind the theory. One thing though: I wouldn`t consider this "The basics" haha, this is 'optional' to PHP and could take people a while to wrap their heads around. Anyway, nice read, couldn`t find any noticeable errors.

Well, 1 thing but that`s not specific to PHP, the return vars shouldn`t have to be stored in variables. So $return = "Some String"; return $return; can just be return "This String"; in that example.

Also, $text = $object->sayThis();
echo $text; can just be :

echo $object->sayThis();

Ph well, that`s my 2 cents.. :D
Baseball435
Posts: 548
Joined: Sun May 30, 2010 3:49 am

Re: [PHP] The Basics: Classes

Post by Baseball435 »

Haha yeah I know that but I wanted the readers to know that you can save the return variable and not have to echo it out

~baseball435
Post Reply

Return to “Tutorials”