小编Ada*_*dam的帖子

文件中的return语句不会停止类定义?

正如php.net/return所述,有一个选项可以使用'return'语句停止执行include.

谁可以告诉我为什么这样做如下所述

// test1.php

include 'test2.php';
var_dump(class_exists('TestClass_ShouldntBeDefined'));
Run Code Online (Sandbox Code Playgroud)

// test2.php

return;
class TestClass_ShouldntBeDefined { }
Run Code Online (Sandbox Code Playgroud)

// 跑

$ php -f test1.php
bool(true)
Run Code Online (Sandbox Code Playgroud)

为什么这是真的?

当test2.php改为任何其他形式的代码时execute(if(true) { ... })

return;
{
    class TestClass_ShouldntBeDefined { }
}
Run Code Online (Sandbox Code Playgroud)

然后它按预期工作

$ php -f test1.php
bool(false)
Run Code Online (Sandbox Code Playgroud)


PHP版本

$ php -v
PHP 5.4.7 (cli) (built: Sep 13 2012 04:20:14) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans
Run Code Online (Sandbox Code Playgroud)

php return include

4
推荐指数
1
解决办法
107
查看次数

标签 统计

include ×1

php ×1

return ×1