PHP在方法中include另一个类

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

知乎网友翻译如下: http://www.zhihu.com/people/damon-yang-15

1、被包含文件的变量的PHP include作用域遵从(不改变)包含文件所在处的作用域。
2、所有在被包含文件中定义的函数和类在被包含后,在包含文件里都具有全局作用域

下面三种方式效果完全一样,已亲测:

include_once 'bar.class.php';
class Foo
{
    public static function DoIt()
    {
        new Bar();
    }
}
vs

class Foo
{
    public static function DoIt()
    {
        include_once 'bar.class.php';
        new Bar();
    }
}

vs
class Foo{
         function load(){
                 include_once 'bar.class.php';
         }
}
$foo = new Foo();
$foo => load();
$bar = new Bar();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值