In order to create a class you simply do the following:
<?php
class name
{
}
?>"class" tells PHP that you are creating a class and then the text after it, in this case "name", tells the classes name. Then it is followed by {} anything inside the curly braces is apart of the name class.
You can define variables inside of your class that can be used for any function inside your class. This is helpful if you are creating something like a template system and want several functions to do changes to one source code.
<?php
class name
{
var $example = '';
}
?>Now you can call the $example variable in any function.
To create a function you need to tell PHP it is a function, give it a name and define any variables the function may need.
<?php
class name
{
var $example = '';
function function_example($var)
{
}
}
?>As you can see we started with function which means it is a function then we made a name for it. For this tutorial we just called it function_example then we have a variable called $var. You do not need a variable I just used one to show you how it worked. If you didn't have a variable it would just be:
<?php
class name
{
var $example = '';
function function_example()
{
}
}
?>But for our example we will use a variable.
Now following the variable we have more curly braces ({}) and anything inside them is apart of the function.
Now we will use that variable to define our $example variable.
In order to call any global variable or function inside the class we use $this->NAME. This means that it is inside of our class.
<?php
class name
{
var $example = '';
function function_example($var)
{
$this->example = $var;
}
}
?>We have now said that $example will be what $var is.
Now lets create another function which will display this data.
<?php
class name
{
var $example = '';
function function_example($var)
{
$this->example = $var;
}
function display()
{
return $this->example;
}
}
?>As you can see there is now a new function called display inside that we use return. Return is used to send data to whatever was calling that function in this case we wanted to send the $example variable back.
This is nice and all but now how do we actually use this class and its functions?
<?php
class name
{
var $example = '';
function function_example($var)
{
$this->example = $var;
}
function display()
{
return $this->example;
}
}
$name = new name;
?>Outside of the class we created a new variable called $name and it equals new name. We use new to assign a class to that variable our class was called name.
Now we are able to use the functions.
<?php
class name
{
var $example = '';
function function_example($var)
{
$this->example = $var;
}
function display()
{
return $this->example;
}
}
$name = new name;
$name->function_example('This is an example');
$name->display();
?>Now we can call our functions we do this by first entering our variable of the class, we used $name. We then follow it by -> and the class name. When you run this nothing will happen because its returned the $example variable but has nothing to do with it.
But, if we echo it we can now see what we entered.
<?php
class name
{
var $example = '';
function function_example($var)
{
$this->example = $var;
}
function display()
{
return $this->example;
}
}
$name = new name;
$name->function_example('This is an example');
echo $name->display();
?>That is just some basic class functionality just to give you a few examples to play with.
Like This Tutorial? Join the Community Today!













