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.
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:
class cellphone {
private $phonenr;
private $color;
}
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:
include('cellphone.php');
$myphone = new cellphone();
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:
class cellphone
{ private $phonenr;
private $color;
public function specs
($nr,
$color) { if(is_numeric($nr)) { $this->
phonenr =
$nr;
} $this->
color =
$color;
} }
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:
$this->functionname();
With that said, we can head back to index.php and set some specs, values to the phone we created.
php Code:
include('cellphone.php');
$myphone = new cellphone();
$myphone->specs(0704153359, 'white');
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:
class cellphone
{ private $phonenr;
private $color;
public function specs
($nr,
$color) { if(is_numeric($nr)) { $this->
phonenr =
$nr;
} $this->
color =
$color;
} public function getdata
() { if(isset($this->
color) AND
isset($this->
phonenr)) { echo 'Your number is: '.
$this->
phonenr.
'<br />Your cellphone is: '.
$this->
color;
} else { echo 'One or more specs are missing! :O';
} } }
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:
include('cellphone.php');
$myphone = new cellphone();
$myphone->specs(0704153359, 'white');
$myphone->getdata();
Really simple.
So what's the advantage of this you ask?
Well, let's say you want to add another phone.
php Code:
include('cellphone.php');
$myphone =
new cellphone
();
$myphone->
specs(0704153359,
'white');
$myphone->
getdata();
echo '<br /><br />';
$yourphone =
new cellphone
();
$yourphone->
specs(0735295832,
'blue');
$yourphone->
getdata();
That will echo out:
Code:
Your number is: 0704153359
Your cellphone is: white
Your number is: 0735295832
Your cellphone is: blue
Ofcourse, 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.