解析thinkphp import 文件內(nèi)容變量失效的問題
更新時間:2013年06月20日 16:14:44 作者:
本篇文章是對thinkphp import文件內(nèi)容變量失效的問題進行了詳細的分析介紹,需要的朋友參考下
用TP 集成支付寶賬戶綁定功能時碰上個問題
ORM 下有文件 config.class.php
直接import()后 發(fā)現(xiàn)里面的變量無法使用 但確實是加載咯。。(在config.class.php輸出內(nèi)容成功)
思考百度了半天。。
原來一直知道 JS 作用域 忽略了 PHP 函數(shù)也有作用域的- -
具體原理:
<?php
class b{
function test(){
myImport("a.php");
$testClass = new impClass();
$testClass->test();
echo $a."from b";
}
}
class a{
function funa(){
$InsB = new b();
$InsB->test();
}
}
function myImport($file){
require $file;
echo $a."from myImport";
}
$InsA = new a();
$InsA->funa();
?>
a.php
<?php
$a = "a";
class impClass{
function test(){
echo "import success";
}
}
?>
顯示:
ORM 下有文件 config.class.php
直接import()后 發(fā)現(xiàn)里面的變量無法使用 但確實是加載咯。。(在config.class.php輸出內(nèi)容成功)
思考百度了半天。。
原來一直知道 JS 作用域 忽略了 PHP 函數(shù)也有作用域的- -
具體原理:
復(fù)制代碼 代碼如下:
<?php
class b{
function test(){
myImport("a.php");
$testClass = new impClass();
$testClass->test();
echo $a."from b";
}
}
class a{
function funa(){
$InsB = new b();
$InsB->test();
}
}
function myImport($file){
require $file;
echo $a."from myImport";
}
$InsA = new a();
$InsA->funa();
?>
a.php
<?php
$a = "a";
class impClass{
function test(){
echo "import success";
}
}
?>
顯示:

相關(guān)文章
PHP sprintf() 函數(shù)的應(yīng)用(定義和用法)
sprintf定義和用法,sprintf() 函數(shù)用于把格式化的字符串寫入一個變量中。2012-06-06php+mysql實現(xiàn)的無限分類方法類定義與使用示例
這篇文章主要介紹了php+mysql實現(xiàn)的無限分類方法類定義與使用,結(jié)合實例形式分析了php+mysql無限分類方法類基本定義與使用方法,需要的朋友可以參考下2020-05-05