ここでのフレームワークは、内部の非OOPに依存しすぎているようです。構築するための好ましい方法ではありませんが、変数のリストを循環し、それらをクラス/インスタンススコープの一部にすることで、必要なことを実行できます。ここでかなり役立つ関数はget_defined_vars()です。
ファイルa.php、b.php、c.phpがあるとしましょう。それぞれは次のようになります。
a.php:<?php $a = "AAAAAA";
b.php:<?php $b = "BBBBBBBBBB";
c.php:<?php $c = "CCCCCCCCCCCCCCCCCCCCCCCCCCC";
class mystuff {
function include_with_vars( $____file ) {
// grab snapshot of variables, exclude knowns
$____before = get_defined_vars();
unset( $____before['____file'] );
// include file which presumably will add vars
include( $____file );
// grab snapshot of variables, exclude knowns again
$____after = get_defined_vars();
unset( $____after['____file'] );
unset( $____after['____before'] );
// generate a list of variables that appear to be new
$____diff = array_diff( $____after, $____before );
// put all local vars in instance scope
foreach( $____diff as $variable_name => $variable_value ) {
$this->$variable_name = $variable_value;
}
}
function __construct($file = NULL){
$this->include_with_vars( "a.php" );
$this->include_with_vars( "b.php" );
$this->include_with_vars( "c.php" );
}
}
$t = new mystuff();
echo "<PRE>";
print_r( $t );
このプログラムは、include()ディレクティブからローカル変数を取得し、それらをクラススコープに配置します。
mystuff Object
(
[a] => AAAAAA
[b] => BBBBBBBBBB
[c] => CCCCCCCCCCCCCCCCCCCCCCCCCCC
)
つまり、ファイルa.php($a
)のローカル変数はになり$t->a
ました。