Home > php教程 > php手册 > body text

解析thinkphp import 文件内容变量失效的问题

WBOY
Release: 2016-06-13 11:47:14
Original
964 people have browsed it

用TP 集成支付宝账户绑定功能时碰上个问题
ORM 下有文件 config.class.php
直接import()后 发现里面的变量无法使用  但确实是加载咯。。(在config.class.php输出内容成功)
思考百度了半天。。
原来一直知道 JS 作用域 忽略了 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
$a = "a";
class impClass{
   function test(){
      echo "import success";
   }
}
?>


显示:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!