Developing classes
The traditional development approach is to place the class into its own file. Typically, classes contain logic that implements a single purpose. Classes are further broken down into self-contained functions which are referred to as methods. Variables defined inside classes are referred to as properties. It is recommended to develop a test class at the same time, a topic discussed in more detail in Chapter 13, Best Practices, Testing, and Debugging.
How to do it...
- Create a file to contain the class definition. For the purposes of autoloading it is recommended that the filename match the classname. At the top of the file, before the keyword
class
, add a DocBlock. You can then define properties and methods. In this example, we define a classTest
. It has a property$test
, and a methodgetTest()
:<?php declare(strict_types=1); /** * This is a demonstration class. * * The purpose of this class is to get and set * a protected property $test * */ class Test { protected...