Learn Classes With Php [Part 1]

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

Learn Classes With Php [Part 1]

Post by Baseball435 »

Hey everyone, this is my first tutorial in the tutorial 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 "indie-resource.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 = "indie-resource.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 "indie-resource.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 indie-resource we would do $ourclass->indie-resource(); 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: indie-resource.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
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Learn Classes With Php [Part 1]

Post by hallsofvallhalla »

nice little tutorial. thanks for posting!
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Learn Classes With Php [Part 1]

Post by Xaleph »

Nice to see some people trying to explain classes.

However, a couple of things to remember:

1. For best practices, capitalize class names. In fact, camelcase with leading capitalization.
2. Use constructors for handling internal fields.
3. If you are going to return something, don`t use a variable, just return it. In the case of sayThis();
4. Filenames should be the same as the class name. IE

class OurClass {} should be saved as OurClass.php. Some find it better to have something like: OurClass.class.php, so beginners understand it`s a class. However, if you are building your own application, I wouldn`t bother, because, if done properly, you`ll know where to store your classes.

5. One minor thing, you said somehwere "if we had a function ( method ) called indie-resource..", any way, the - ( dash ) is not allowed in function names / method names. It`s a PHP operator, so you cannot really use it.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Learn Classes With Php [Part 1]

Post by Jackolantern »

While I agree that the usage you suggest in #3 should be mentioned, I don't know that it is necessarily something that needs to be changed. In fact, from what I have seen beginners seem to get along better when they can easily that a variable is being used. When you start directly using returned values, novice devs who are still coming to grips with programming can start to get confused because a little bit of clarity has been stripped away. When they see a variable, they immediately know that a value is being passed around in there somehow, and they can go from there. It would also make a trivial performance increase for such short-lived variables as those found in typical PHP scripts.

All-in-all, a good tutorial :) It is hardly ever too early to start learning OOP!
The indelible lord of tl;dr
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Learn Classes With Php [Part 1]

Post by Xaleph »

True, but then again, if you are going to return a var like that, it`s useless. Why bother? Normally you return a (private) field in the object. But when learning OO, you really need to learn the whole return method, var swapping is not good. True, the performance is not worth mentioning, but it`s not about performance, it`s about code style. Unless you use the var for mutation, honestly, there`s no need for it. Vars should be used wisely. However, you have a point. In PHP it doesn`t actualy matter. Most vars are swapped anyway. It only registers new vars if old ones still contain data. Otherwise the space for "used" vars is used. Too bad you have little GC for PHP, but then again, PHP manages it on it`s own.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Learn Classes With Php [Part 1]

Post by Jackolantern »

There really isn't any need for much garbage collection in PHP since the script is gone and nearly all resources are reacquired within milliseconds of starting the script. In a way, you could say it has the most strict GC out there ;)

And I know what you mean about style. But I feel that novices can always learn something like PEAR style later once they get a grip on the basics. Whatever makes it clear in the beginning is what works, and I think it is the natural progression to compress your own code further and further as you learn more.
The indelible lord of tl;dr
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Learn Classes With Php [Part 1]

Post by Xaleph »

Yeah you are right. However, I rather stick to the fundamentals. In Dutch we have an old saying "jong geleerd, oud gedaan", which translates to something like "learn young, know later."Basically what is means is, better learn it when everything is fresh and new, when you`re just starting out, because you`ll have a greater advantage later on. And you know what they say, old habits die slow.
Post Reply

Return to “Tutorials”