If I have:
require_once("bla.php");
class controller{.....}
If I then create in a different file class control_A extends controller{...}, do I need to again say require_once("bla.php");, or is it inherited?
What if the require_once is done inside the class controller definition?
So far we have two contradicting, but equally correct answers =) Let’s see if I can’t combine the two into a more specific total answer.
If any class requires any code or definitions in
bla.php, then you will need toinclude("bla.php")at least one time in the entire run-time of your script. If your previous code:is in the file
controller.phpthen you can createcontrol_Ain the following way:This is because the
require_once()function essentially copies and pastes the contents of the file into the script at that line. Therefore the above will be seen equivalent to this:As you can see, just by requiring
controller.php, the necessary definitions for controller are now seen and parsed. What you can not do is omit the declaration ofcontroller. This isn’t just because you requiredbla.phpwhile declaring it, but also you can’t extend a class which hasn’t yet been declared. So the following code:will give you an error since controller hasn’t been defined.
One thing to consider, however- since the class
controllerdoesn’t extend any other class, it should not have any external dependencies. There’s a good chance that whatever you do inbla.phpwhich must run before defining the class is either unnecessary or can be restructured. What exactly isbla.phpdoing that you need before definingcontroller?