![]() |
[Tutorial] Basic tutorial about class basics
Class basics
Hello, and welcome to this really basic tutorial. It's a basic tutorial, about the basics in classes. So I want to keep this as simple as possible so you can understand. Let's begin with some words about classes. Classes are a like a bunch of functions, that provides us with a set of options that we can use. So obviously, in order to understand this tutorial, you have to understand how functions works. Classes are not a blueprint of what functions you need to use. Classes are only there to provide you with options that you CAN use. Also, one thing worth mentioning, is that you need a somewhat new version of php-server in order for it to read the class correctly. So with that said, let's check out the first thing you need to know when starting classes, which is ofcourse how to start the class. php Code:
As you see, we just created a class called cellphone. This is how you create a class, it's THAT simple. Everything within those brackets will be within the class, and can only be used when you've called the class(which I will go through later). So let's add some variables in the class. Let's say for example you want to store what color the cellphone has, in one variable. And what phonenumber it has, in another variable. php Code:
As you see, we don't set a value of these variables, we only put them out there. And also we used the PRIVATE option on those variables. I won't go through the different options you can put on variables(and functions!), because I think it's a little more advanced, so let's just keep the variables as private. So go ahead and save this file as cellphone.php. Then go make a index.php where you include the cellphone.php. index.php php Code:
As you see, we also create a cellphone! We assign an instance of the class cellphone, into the variable $myphone. So basicly, $myphone now contains everything within this class, which isn't anything basicly right now. So let's go back to cellphone.php and create a function. php Code:
Whoa! Now we had alot of stuff here. First of all, we created a function called specs, and we used PUBLIC option on this, because.. well, just because. We also have $nr and $color in that function which we will set when calling the function. Inside the function we just check if the $nr is actually indeed a number, and if it is we assign its value into $this->phonenr. So wait a sec.. what's $this-> ??? Well, because the class doesn't know if it's been called yet(created), we have to use $this to point that it's in THIS class we want to use something. So that's with everything within the class. If you want to use a function in the class, from INSIDE the class, you have to use PHP Code:
php Code:
So, we've just set some values to the phone. And if you look at index.php in your browser, when the server is online ofcourse, it will show up as blank(if you've done correct). This is GOOD, because we haven't actually written out anything. Let's go back to cellphone.php We have to create a function that will retrieve the data from the phone. php Code:
So it's pretty basic here. We created the function getdata. We check if the variables are set and if they aren't, we write out that one or more is missing. If they both are set, we write out the values. Now all we have to do is to call the function from index.php php Code:
Really simple. So what's the advantage of this you ask? Well, let's say you want to add another phone. php Code:
That will echo out: Code:
Your number is: 0704153359Ofcourse, this is just basic stuff, and I'm almost certain that noone want to use a class to do this simple. But classes are very handy when for example creating a news system. So this was all from me, I hope you learned something. And ask if you have any questions. |
Not one comment even? :(
|
Great tutorial, Tanax keep it up when is the next tutorial coming ?
|
Quote:
Quote:
Quote:
Do a little more research before taking novice users into the world of object orientation? |
Quote:
Quote:
Quote:
Cut him a little slack, he may well have been doing the tutorial to learn as much for himself as to help others, and either way he didn't do a bad job. You are right though that everybody should do research before diving in, that's never bad advice. -m |
Quote:
And it makes our life easier WITH a set of provided functions. And actually I said "Classes are LIKE a set of...". I didn't say it WAS a set of functions. Quote:
In order to use this guide correctly, you need a somewhat newer version of PHP. Quote:
Thus, I said that we only put them there, WITH NO VALUE, because that's what I did. Quote:
|
Great article I will link to this so that other people will know there is a great article on Classes on the web.
Thanks |
Quote:
|
Quote:
Quote:
|
lol. No worries. Good read anyways :)
|
Good tutorial, thanks man. :-)
|
Quote:
Quote:
|
Great post!
This is a BASIC introduction people. Starter don't need to know exact terminology; that would be too much to ask. It got it's point across to me!
Awesome article! Can't wait for another! |
Nice work m8.
Quote:
Quote:
Whilst it may be good to be 'technical' and 'picky' about names of class/object functions/variables for a basic tutorial I hardly think its necessary and can be confusing for a new OOP'er to start abstracting names for functions (remember they are still declared using 'function') for a tutorial of this scope its unhelpful. Aslong as the basic facts are underlined and understood by the reader its done its job. |
Quote:
Quote:
|
| All times are GMT. The time now is 07:56 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0