【问题标题】:Is there an equivalent of "let" vs. "var" in PHP?PHP中是否有“let”与“var”的等价物?
【发布时间】:2017-04-21 08:42:13
【问题描述】:

我最近开始学习 Swift 进行 iOS 开发。我有脚本语言的背景,尤其是 PHP。看到强调使用let 定义一个常量以支持var 让编译器优化生成的代码,我想知道:PHP 是否有等价物?或者它根本不适用,因为 PHP 不是静态编译的?

我在搜索时尝试了运气,但没有找到令人满意的信息。

【问题讨论】:

  • PHP has constants 通过define('CONSTNAME', 'constant value')const CONSTNAME = 'constant value' in a class 定义。这是你想知道的吗?
  • @MichaelBerkowski 来自 Apple aisde 的狡猾命名法,let 变量不是常量,它们是不可变变量。它们在运行时被初始化,在看到定义的地方(如果重新进入定义它们的块,可能不止一次)。
  • 例如,如果 x 是函数参数,let y = x + 1 是完全合法的,并且每次调用函数时都会给 y 一个不同的值(但不能在函数)。
  • @hobbs:很有趣。我还没有意识到这一点。
  • @MichaelBerkowski:听起来对我来说是正确的。这就是我在 PHP 中使用它们的方式。考虑到 hobbs 的评论,我需要澄清一下,我想知道 PHP 中是否存在不可变变量与常规变量之类的概念。

标签: php swift


【解决方案1】:

不,您不能在 PHP 中使用本地范围的常量。所有 PHP 常量总是全局可见的。也没有像不可变/可变变量这样的概念。

您可以实现不可变对象成员 (PHP: immutable public member fields),但这是另一回事。

实际上语言中有一个const 关键字,但文档说:

注意:

与使用 define() 定义常量相反,使用 const 关键字定义的常量必须在顶级范围内声明,因为它们是在编译时定义的。这意味着它们不能在函数、循环、if 语句或 try/catch 块中声明。

(来自http://php.net/manual/en/language.constants.syntax.php

具有动态类型系统的解释语言可以有类似 swift let 语句的东西,所以这不是因为编译了 swift 而解释了 PHP(例如,有一个 javascript 提议介绍该功能:https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Statements/const)

【讨论】:

  • 感谢您的信息。进一步澄清,我想知道 PHP 中是否存在不可变变量。
【解决方案2】:

PHP 中是否存在“let”与“var”的等价物?

PHP 还没有 let 作为本地语言功能(截至当前版本 7.1.4 - 04/2017)

但是,PhalconIce 等一些高性能扩展支持let,因为zephir-lang 的底层用法。 所以,有let,但是是间接的;使用上述扩展。

有两个用例:


例如,看看Ice Router的来源:

namespace Ice\Mvc\Route;

use Ice\Mvc\Route\Parser\ParserInterface;
use Ice\Mvc\Route\DataGenerator\DataGeneratorInterface;
use Ice\Mvc\Route\Parser\Std;
use Ice\Mvc\Route\DataGenerator\GroupCount as Generator;

class Collector
{

    private routeParser { set };
    private dataGenerator { set };

    /**
     * Constructs a route collector.
     *
     * @param RouteParser   $routeParser
     * @param DataGenerator $dataGenerator
     */
    public function __construct(<ParserInterface> routeParser = null, <DataGeneratorInterface> dataGenerator = null)
    {
        if !routeParser {
            let routeParser = new Std();
        }

        if !dataGenerator {
            let dataGenerator = new Generator();
        }

        let this->routeParser = routeParser,
            this->dataGenerator = dataGenerator;
    }

    /**
     * Adds a route to the collection.
     *
     * The syntax used in the $route string depends on the used route parser.
     *
     * @param string|array $httpMethod
     * @param string $route
     * @param mixed  $handler
     */
    public function addRoute(var httpMethod, string route, handler = null)
    {
        var routeDatas, routeData, method;

        let routeDatas = this->routeParser->parse(route);

        if typeof httpMethod == "string" {
            let method = httpMethod,
                httpMethod = [method];
        }

        for method in httpMethod {
            for routeData in routeDatas {
                this->dataGenerator->addRoute(method, routeData, handler);
            }
        }
    }

    /**
     * Returns the collected route data, as provided by the data generator.
     *
     * @return array
     */
    public function getData()
    {
        return this->dataGenerator->getData();
    }
}

【讨论】:

    猜你喜欢
    • 2019-04-04
    • 2010-09-24
    • 1970-01-01
    • 2010-11-15
    • 2012-11-17
    • 2019-03-11
    • 2021-08-04
    • 1970-01-01
    • 2011-11-29
    相关资源
    最近更新 更多