Classes are great, but how do we use them? As with any programming language, the classes have to be instantiated, and the objects have to be created. The first step required is to require the file.
PHP Code:
<?php
require_once("HelloWorld.class.php")
?>
This assumes we have a php class named HelloWorld.class.php. As a side note, it is a convention to name your class files with the .class extension, however since they are usually listed in public directorys on the internet, adding the .php extension after the .class extension prevents your code from being viewable, which is an extra security precaution you should take.
Anyway, once we have included the contents of the file, we want to instantiate it. We do this by using the new keyword, moreover you usually asign it a variable:
PHP Code:
$hello = new HelloWorld();
Now if the HelloWorld class contains public functions we can call then using " -> ". Lets assume the HelloWorld class has a function called HelloCodeCall that looks like this:
PHP Code:
function HelloCodeCall() {
echo "Hello CodeCall users!";
}
To call that function you do this:
PHP Code:
<?php
require_once("HelloWorld.class.php")
$hello = new HelloWorld();
$hello->HelloCodeCall();
?>
And that will print the content of the constructor and the content of HelloCodeCall.