from my understanding, require pastes code into the calling php file.
what if you were requiring from inside a method…it would paste the entire code/class inside the method, blocking the next statement in the method.
eg.
function test() {
require 'pathtosomeclasscode';
somestatement; // any code after the require is blocked.
}
how do i get around this, to be able to require code where-ever, without it being pasted in that exact spot?
Somewhat surprisingly, this appears to work just fine. The included code will execute inside the scope of Baz::bork() — but the the code simply defines a class, and classes are global. So you end up with a defined class.
File: Foo.php:
File: Baz.php:
Output:
Now, I can’t think of many places where you’d want to do things this way. People typically
require_once()any possible dependencies at the top of their class file, outside of the class definition.