What does if (\ false) (yes, with a backslash) mean in PHP?

This morning I was notified that a new Twig_Extensions release is available ! Hooray!

Before integrating it into twigfiddle , I wanted to see the changes . This basically adds namespace support with class_alias to the class_alias function, and then adds PSR-4 classes with the corresponding class_alias , which simply include the inherited class.

But each new (namespaced) class is implemented like this:

 <?php namespace Twig\Extensions; require __DIR__.'/../lib/Twig/Extensions/Extension/Text.php'; if (\false) { class TextExtension extends \Twig_Extensions_Extension_Text { } } 

What does this designation mean?

+8
source share
4 answers

This means that it uses the false value defined in the global namespace.

After a little research, it turns out that the rest of this answer is not subject ... I can swear that you were able to do this in PHP at some point in time.

I think it’s getting around a situation where

 <?php namespace whywouldyoudothis; false = true; ?> 

I have never seen anyone code for this, but this is what comes to mind.

+5
source

From php manual

A prefix with \ will indicate that the name is required from the global space, even in the context of a namespace.

+3
source

This code is meaningless, just this code is not available, because \ false is always false!

0
source
 if (\false) { class TextExtension extends \Twig_Extensions_Extension_Text { } } 

Code is still available for code analyzers and IDEs. However, I think there should be a note about obsolescence. So developers will be notified of the use of obsolete classes.

Here is an example from the main Twig repository. https://github.com/twigphp/Twig/blob/v2.10.0/lib/Twig/Token.php

0
source

Source: https://habr.com/ru/post/1268161/


All Articles