話を簡単にしたいので、ソースはあくまでサンプルで。
呼び出し元
http://www.example.dom/test.php
別サーバー
http://lib.example.dom/
includeしたいファイル
http://lib.example.dom/hoge.php
<?
class person() {
public $name;
public $addr;
public $age;
function __construct() {
$this->name = "";
$this->addr = "";
$this->age = "";
}
}
$o_person = new person();
$o_person->name = "hogehoge";
?>
呼び出し元のソース
<? include 'http://lib.example.dom/hoge.php'; echo "o_person = "; var_export($o_person); ?>
で、結果はこうなる。
o_person = NULL
外部ファイルを拡張子「.php」のままincludeすると、外部ファイルでphpが「実行」されその「結果」が返される。
そのため、変数$o_personは別サーバー上にしか存在しえない。
PHPのソースとしてincludeしたいため、「結果」として「ソース」が返ってくればよい。
ということで、外部ファイルの拡張子を「.php」から「.inc」に変えたら、見事成功!
呼び出し元のソース
<? include 'http://lib.example.dom/hoge.inc'; // hoge.php を hoge.inc に変更。これで、呼び出し先の外部サーバーでPHPとして実行されなくなる。 echo "o_person = "; var_export($o_person); ?>
o_person = person::__set_state(array( 'name' => 'hogehoge', 'addr' => '', 'age' => '', ))
ただし、呼び出し先でさらにincludeしていると、期待した結果にならないので注意。
0 件のコメント:
コメントを投稿