Classes versus objects
A PHP class is a file that contains codes. Your class is a file that physically exists on your drive. If you turn your computer off and turn it on again, the class file is still there. The code it contains is a template to create an object during execution. The class file can contain properties and behaviors. The properties will be able to hold data in memory once the class has been instantiated. The behaviors will be handled by methods or functions. You can use the accessors and mutator methods to change the values of a class’s properties.
Dog.php class file
<?php namespace Animal; class Dog { public function returnSound(): string { return "Bark"; } }
The preceding example class is a PHP file containing the namespace declaration, the class name, and a single method or function called returnSound()
, which returns...