PHP #region for code folding?

As everybody has said before, the #region in .Net is actually a Visual Studio feature, not a C# one, but the fact that the C# grammar supports the syntax, allows its usage by any IDE that wants to implement it. Since in PHP, the '#' character is also used for comments, the same can be done by the IDEs. In fact, JetBrains' PhpStorm does it: https://blog.jetbrains.com/phpstorm/2012/03/new-in-4-0-custom-code-folding-regions/


No.

The thing is, C# is sort of designed to be written by only one IDE, because Microsoft need you to always use their tools. So, built into the language (sort of) are things that affect the IDE.

PHP, on the other hand, is just a language. It's not supposed to have a symbiotic relationship with some specific editor, so it doesn't. So, no, it has nothing to control your editor.

Still, all proper programming text editors support folding class definitions, function definitions, and most scope blocks. Some editors may allow certain extensions to go beyond this.


No, there's nothing directly in the language.

But every decent editors allow some kind of markup to allow this.

For example in Netbeans :

// <editor-fold defaultstate="collapsed" desc="user-description">
  ...any code...
// </editor-fold>

This syntax also works in all the Intellij IDEA editor family, see http://blog.jetbrains.com/webide/2012/03/new-in-4-0-custom-code-folding-regions/

You can also emulate the feature in Eclipse via a plugin : Code folding plugin for Eclipse?

Visual Studio Code includes it since version 1.19 :

#region user description
...any code...
#endregion

There is no equivalent (the other answers explain why), but let me show you how I do it:

//////////////////////////
/* REGION A */ {

    function SomeFunction() {
        return true;
    }

    function AnotherFunction() {
        return false;
    }

}

//////////////////////////
/* REGION B */ {

    function ThirdFunction() {
        return true;
    }

    function FourthFunction() {
        return false;
    }

}

The curly braces allow me to fold the block of code (your editor will need to support cold folding, but almost all of them do), while I still see the region name and have a simple visual divider.

Folded Result:

//////////////////////////
/* REGION A */ { 

//////////////////////////
/* REGION B */ { 

Tags:

Php