包括来自静态类方法的php文件


including php file From a static class method

我有两个php文件。我无法从A的静态方法中获得B的全局变量:

A.php

class c_A
  { public static function f_A()
      { include_once( "B.php" ) ;
        print f_B() ;
      }
  }
c_A::f_A( ); // only prints "B : "

B.php

$gvs = "global variable from B" ;
function f_B()
  { return "B : " . $GLOBALS[ "gvs" ] ;
  } 

$GLOBALS[ "gvs" ]为空,因为您在函数内部调用B.php。所以$gvs变量并没有声明为全局变量。如果你在A.php的课外包含B.php,你会得到结果:

A.php

include_once( "B.php" ) ;
class c_A
  { public static function f_A()
      { 
        print f_B() ;
      }
  }
c_A::f_A( ); // will prints "B : global variable from B"