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?
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_onceof the base class file, or autoloading (there are other options for autoloading, butspl_autoload_registeris 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: