0

I cannot find any hint about the fact I cannot import any magic constant.

Trying to like ...

<?php declare( strict_types = 1 );
namespace CodeKandis\MyVendor;

use function dirname;
use const __DIR__;

require_once dirname( __DIR__ ) . '/vendor/autoload.php';

... leads to

Parse error: syntax error, unexpected '__DIR__' (T_DIR), expecting identifier (T_STRING) or \\ (T_NS_SEPARATOR) in /public/index.php on line 5

This question is important while PHPStorm tends to auto import magic constants. And if it's not possible this needs to be reported and fixed.


Edit (2019-07-25)

After I opened an issue this will be fixed in PHPStorm 2019.3.

codekandis
  • 712
  • 1
  • 11
  • 22
  • 1
    you don't need to re-declare a magical constant - just use it in your scripts – B001ᛦ Jul 08 '19 at 09:20
  • @B001ᛦ To be clear we don't misunderstand: I see a difference between an import and a redeclaration. The import of functions and constants is recommended for micro optimization. The main issue here is PHPStorm imports magic constants which doesn't seem to be possible. – codekandis Jul 08 '19 at 09:27
  • 1
    _I see a difference between an import and a redeclaration.._ Even if you see there is a difference, this doesn'tchange the fact that you can use magical constants without importing or redeclaring or what ever – B001ᛦ Jul 08 '19 at 09:28
  • Thx. Ok then. I know I don't need to. But my question is "can magic constants be imported?" – codekandis Jul 08 '19 at 09:30
  • 2
    No they are magic constants. There is no where to import them from. – Dharman Jul 08 '19 at 09:40
  • @dharman So "no where to import from" means magic constants are not namespaced? – codekandis Jul 08 '19 at 09:41
  • 1
    Try it yourself: https://3v4l.org/R1oTq – Dharman Jul 08 '19 at 09:43
  • Ahh, thx. I didn't know about that differenziation. – codekandis Jul 08 '19 at 09:44
  • If I have time in the evening maybe I will post a more detailed explanation. – Dharman Jul 08 '19 at 09:45
  • That would be nice. I was about to answere this question myself and to quote you. But I'm curious about the details. So I'll wait. – codekandis Jul 08 '19 at 09:50

1 Answers1

1

In PHP only OOP stuff (classes, interfaces, exceptions, errors...) must be fully named-qualified. If you do not specify full name or do not import class into another namespace, PHP will not fallback to global namespace to look for it.

You can also use fully-specified functions or constants. Functions can belong to a namespace, and in fact all core functions belong to global namespace i.e. \. PHP will first look for the function in the current namespace and it will fall back to global functions or constants if a namespaced function or constant does not exist. You can perform micro-optimization if you specify the global namespace explicitly, because PHP will look in the global namespace directly.

namespace A {
    function phpinfo(){
        echo 'I am bogus';
    }
    phpinfo(); // vs. \phpinfo()
}

Magic constants are not really constants at all. They change value based on the context. They are more like magic variables. The following code is invalid, because these constants do not belong to any namespace, not even the global one.

namespace A {
    echo \__LINE__;
}

At compile time PHP will substitute them with the actual values. They can't be imported either for exactly the same reason, they are not defined anywhere, they are simply an instruction for the compiler.

There are also other things which can't be imported or namespaced, see: List of Keywords.

You cannot use any of the following words as constants, class names, function or method names.

namespace A {
    \echo 'hi'; // <-- this line is invalid code
    \die(1); // neither is this, even if it looks and behaves like a function
}

Some people confusingly put parentheses after echo or print, treating them like functions, but in reality they are not. The ones listed with parentheses behave like functions accepting parameters, but you can't import them either.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • That's a detailed explanaition. I know the difference between regulary constans and magic constants. But the fact they're not namespaced is new to me. That the mentioned functions (lets call them keyword functions(?)) aren't namespaced isn't new to me either. In conclusion PHPStorm doesn't handle the imports in the right way. Good to know and worth an issue. – codekandis Jul 08 '19 at 20:40
  • 1
    They are called keywords or language constructs. They are part of the language just like `+` or `;` is. They are neither functions, constants nor classes. – Dharman Jul 08 '19 at 20:43
  • I just wanted to make a difference between keywords like a `return` or `break` statement, which are called without parentheses and keywords used like functions with parentheses. – codekandis Jul 08 '19 at 20:47