【问题标题】:What's the execution flow of below PHP scripts? [duplicate]以下PHP脚本的执行流程是什么? [复制]
【发布时间】:2014-10-24 07:15:23
【问题描述】:

我有一个 test.php:

<?php

include "MyString.php";

print ",";
print strlen("Hello world!");  

还有一个 MyString.php:

<?php

namespace MyFramework\String;

function strlen($str)
{
    return \strlen($str)*2; // return double the string length
}
print strlen("Hello world!");

test.php 的输出是 24,12,但是输出是怎么产生的呢? test.php的执行流程是什么?

还有一个反斜杠:

namespace MyFramework\String;

function strlen($str)
{
    return \strlen($str)*2; // return double the string length
}

这个'\'是否意味着使用命名空间?

【问题讨论】:

  • 我不知道为什么有人给这个问题一个减号,但这是一个关于 PHP 命名空间的问题,这是 PHP 5.3 之后的一个新特性。如果你觉得这个问题很无聊或者没用,请告诉我你的观点
  • 我没有投反对票,但仅供参考,PHP5.3 已经过时并且已经过时了。

标签: php


【解决方案1】:

test.php 的执行包括MyString.php 并在MyString.php 中执行主级代码

MyString.php 打印 strlen 在其自己的命名空间中 的结果(即从全局命名空间调用 MyFramework\String\strlen() 而不是标准的 PHP strlen() 函数)输出24 (12*2).... 然后MyString.php 终止并将控制权返回给test.php

test.php 接下来打印一个逗号,然后打印使用标准 PHP strlen 的 strlen() 的结果,因为它不再在 MyFramework\String 命名空间中执行,因此输出为 12

MyString.php 中命名空间 strlen() 函数中 strlen 之前的 \ 表示使用标准(根命名空间级别)strlen() 函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 2018-05-11
    • 2014-03-01
    相关资源
    最近更新 更多