PHP Classes and Functions

PHP classes are part of object-oriented (as opposed to procedural) PHP programming. They store reusable code — just like CSS classes — and are used for representing data as objects that can contain different types of variables (properties). Here is a very short and very simple example of how to use a function (method) inside of a PHP class:

class myClass {
public function myClassFunction($firstName, $lastName) {
echo '<p>Hello ' . $firstName . ' ' . $lastName . '</p>';
}
}
// Call an instance of the class
$myClassInstance = new myClass;
// Call a function from the class
$myClassInstance->myClassFunction('Carlos', 'Ruiz');

This will output “Hello Carlos Ruiz.”

In this example it would make more sense to just use the function by itself, however, classes are great for organization in larger web applications. As mentioned before, classes are objects that can contain many different types of properties and methods — offering more flexibility if your project is fairly robust.