4

I have recently started with learning Swift for iOS development. I have a background in scripting languages, especially PHP. Seeing that it is emphasized using let to define a constant in favor of var to have the compiler optimize the resulting code, I wondered: is there an equivalent for PHP? Or does it simply not apply as PHP is not statically compiled?

I tried my luck at searching but found no satisfying information on that point.

herrbischoff
  • 3,294
  • 1
  • 29
  • 50
  • [PHP has constants](http://php.net/manual/en/language.constants.php) which are defined via `define('CONSTNAME', 'constant value')` or via `const CONSTNAME = 'constant value'` [in a class](http://php.net/manual/en/language.constants.php). Is that what you wanted to know? – Michael Berkowski Jan 11 '15 at 19:38
  • 3
    @MichaelBerkowski Dodgy nomenclature from Apple aisde, `let` variables aren't constants, they're immutable variables. They're initialized at runtime, at the point where the definition is seen (and potentially more than once, if the block where they're defined is re-entered). – hobbs Jan 11 '15 at 19:52
  • For instance if `x` is a function parameter, `let y = x + 1` is entirely legal and will give `y` a different value each time the function is called (but it can't be reassigned within the function). – hobbs Jan 11 '15 at 19:56
  • @hobbs: Interesting. I haven't gotten to realizing that yet. – herrbischoff Jan 11 '15 at 20:00
  • @MichaelBerkowski: Sounds about right to me. That's how I use them in PHP. Considering hobbs comment, I'd need to clarify that I'd like to know if there is a concept like immutable variables vs. regular variables in PHP at all. – herrbischoff Jan 11 '15 at 20:05

2 Answers2

6

No, you can't have locally scoped constants in PHP. All PHP constants are always globally visible. There is also no concept like immutable/mutable variables.

You can implement immutable object members (PHP: immutable public member fields), but it's a different thing.

Actually there is a const keyword in the language, but the docs say:

Note:

As opposed to defining constants using define(), constants defined using the const keyword must be declared at the top-level scope because they are defined at compile-time. This means that they cannot be declared inside functions, loops, if statements or try/ catch blocks.

(from http://php.net/manual/en/language.constants.syntax.php)

Interpreted languages with a dynamic type systems can have something like the swift let statement, so this is not because swift is compiled and PHP is interpreted (for example, there is a javascript proposal to introduce that feature: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Statements/const)

Community
  • 1
  • 1
stefreak
  • 1,460
  • 12
  • 30
2

Is there an equivalent of “let” vs. “var” in PHP?

PHP doesn't have let as a native language feature, yet (as of current version 7.1.4 - 04/2017)

But, some high-performance extensions like Phalcon and Ice have support for let, because of the underlying usage of zephir-lang. So, there is let, but indirectly; using the above mentioned extensions.

There are two use cases:


As an example take a look at the source for the 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();
    }
}
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141