Background: I\'m building an automated test framework for a PHP application, and I need a way to efficiently \"stub out\" classes which encapsulate communicatio
Okay so behavior of the return statement in PHP included files is to return control to the parent in execution. That means the classes definitions are parsed and accessible during the compile phase. For instance, if you change the above to the following
a.php:
foo();
?>
b.php:
foo();
?>
z.php:
then php a.php
and php b.php
will both die with syntax errors; which indicates that the return behavior is not evaluated during compile phase!
So this is how you go around it:
z.php:
z-real.inc:
z-mock.inc:
Now the inclusion is determined at runtime :^) because the decision is not made until $z_source
value is evaluated by the engine.
Now you get desired behavior, namely:
php a.php
gives:
Fatal error: Cannot redeclare class Z in /Users/masud/z-real.inc on line 2
and php b.php
gives:
This is foo() from the z-real.inc.
Of course you can do this directly in a.php or b.php but doing the double indirection may be useful ...
Having SAID all of this, of course this is a terrible way to build stubs hehe for unit-testing or for any other purpose :-) ... but that's beyond the scope of this question so I shall leave it to your good devices.
Hope this helps.