PHP #region for code folding?

前端 未结 15 1423
星月不相逢
星月不相逢 2021-01-31 14:06

Is there an equivilent of c#\'s #region in PHP?

相关标签:
15条回答
  • 2021-01-31 14:42

    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
    
    0 讨论(0)
  • 2021-01-31 14:42

    You might be able to just use curly brackets {}, but it really depends on your editor.

    I know some editors might pick that up as a code block, allowing you to collapse it, but it probably won't allow you to name the section that you're collapsing purely because that function isn't native to the language.

    0 讨论(0)
  • 2021-01-31 14:48

    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/

    0 讨论(0)
  • 2021-01-31 14:48

    In Visual Studio Code, you can "feel at home" with c# style regions like this:

    #region Constants
        const ICEBERGS_PER_TITANIC = 100000000;
        const COFFEE_IV_DRIP_RATE = 0.00005;
    #endregion
    

    Technically, the region Constants isn't required, all it needs is the # sign. But for c# developers who are used to seeing this (because Visual Studio insists on it being this way), it's not really noisy. And it's actually self-documenting. A PHP maintenance programmer would not by stymied by this.


    Having said that, these ways also work, but it would be confusing (or just less clear) and might not work in another editor:

    #deathstar Constants
        const ICEBERGS_PER_TITANIC = 100000000;
        const COFFEE_IV_DRIP_RATE = 0.00005;
    #deathstar
    
    # Constants
        const ICEBERGS_PER_TITANIC = 100000000;
        const COFFEE_IV_DRIP_RATE = 0.00005;
    #
    
    0 讨论(0)
  • 2021-01-31 14:51

    PhpStorm supports code folding similar to C#:

    #region [comment about what the code does]
    
    code....
    
    #endregion
    
    0 讨论(0)
  • 2021-01-31 14:52

    Now you can use #region on VSCode https://twitter.com/daviwil/status/914925309220106241

    0 讨论(0)
提交回复
热议问题