When we include files in PHP, they are somehow cached. So, if one contains a class definition, when we try to include it twice, we will get an error saying "Can not redeclare class".
But is it possible to get include file code invoked with a scope set to current function, let's say?
E.g. if we have two files:
moo.php:
<?php
class Moo
{
function __construct()
{
echo "hello, world!" . PHP_EOL;
}
}
?>
main.php:
<?php
function foo()
{
include("moo.php");
new Moo();
}
foo();
new Moo(); // <-- here should be an error saying "could not find class Moo"
include("moo.php");
new Moo(); // <-- and here should not
?>
As far as i've tried, not eval(file_get_contents("moo.php"));
, nor namespaces either gave no expected effect with a least of code...