Does extending a parent class in PHP require the file with the class being included?
inheritance, oop, php, require
Solution
You need to do something in order to let PHP see your base class definition before it can process the child class, otherwise a fatal error will occur.
This something can be either a manual `require_once` of the base class file, or autoloading (there are other options for autoloading, but `spl_autoload_register` is the one you should use).
Which approach to use depends on the scope: when coding a small test project setting up autoloading is probably overkill. But as the code base gets larger and larger, autoloading becomes more attractive because:
- it can hide complex source file resolution logic (e.g. if you have a configurable directory for the base classes; there are more advanced scenarios as well)
- it can be configured incrementally: you can use multiple autoloaders that run as a chain, and each independent module of the application can register its independent autoloader that coexists peacefully with all the others
Problem
Should I include/require_once the parent class that I am extending in PHP? for example I have a class called Shapes ``` class Shapes { } ``` And then I created a subclass called ``` require_once('shapes.php'); class Circle extends Shapes { } ``` Should I require the parent class when I am extending? or should just use extends the subclass to itss parent class even though they are in the same folder?