Is there an equivilent of c#\'s #region in PHP?
Workaround
- if(true){
// some code
}
so you can click on dash to collapse the section. Another possibility:
$region = 'load_parameters';
if($region == 'load_parameters'){
// some code
}
I'm using Geany in Linux to do my PHP Programming, and it DO support the #region and #endregion to mark regions of code, and it works just like the MS Visual Studio to in C# language.
PHP Comments can be done using //, /* and */, and # too, so the PHP will just bypass these lines started with # and it's up to your text editor/IDE to make use of this... And Geany does!
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 */ {
I know it's an old thread, but in case you are interested.
I have successfully achieved #region / #endregion code folding in emacs, by adapting this code sample to php mode: http://blogs.msdn.com/b/dotnetinterop/archive/2008/04/14/making-hideshow-el-work-with-csharp-mode-el-and-region-endregion.aspx
The result is here:
; ===== PHP style region folding
(defun php-hs-forward-sexp (&optional arg)
"I set hs-forward-sexp-func to this function.
I found this customization necessary to do the hide/show magic in PHP
code, when dealing with region/endregion. This routine
goes forward one s-expression, whether it is defined by curly braces
or region/endregion. It handles nesting, too.
The forward-sexp method takes an arg which can be negative, which
indicates the move should be backward. Therefore, to be fully
correct this function should also handle a negative arg. However,
the hideshow.el package never uses negative args to its
hs-forward-sexp-func, so it doesn't matter that this function does not
do negative numbers.
The arg can also be greater than 1, which means go forward
multiple times. This function doesn't handle that EITHER. But
again, I haven't see that as a problem."
(message "php-hs-forward-sexp, (arg %d) (point %d)..."
(if (numberp arg) arg -1)
(point)
)
(let ((nestlevel 0)
(mark1 (point))
(done nil)
)
(if (and arg (< arg 0))
(message "negative arg (%d) is not supported..." arg)
;; else, we have a positive argument, hence move forward.
;; simple case is just move forward one brace
(if (looking-at "{")
(forward-sexp arg)
; The more complex case is dealing with a "region/endregion" block.
; We have to deal with nested regions!
(and
(while (not done)
(re-search-forward "^[ \\t]*#[ \\t]*\\(region\\|endregion\\)\\b"
(point-max) 'move)
(cond
((eobp)) ; do nothing if at end of buffer
((and
(match-beginning 1)
;; if the match is longer than 6 chars, we know it is "endregion"
(if (> (- (match-end 1) (match-beginning 1)) 6)
(setq nestlevel (1- nestlevel))
(setq nestlevel (1+ nestlevel))
)
)))
(setq done (not (and (> nestlevel 0) (not (eobp)))))
); while
(if (= nest 0)
(goto-char (match-end 2))
)
))
)
)
)
(unless (assoc 'php-mode hs-special-modes-alist)
(push '(php-mode
; "\\(^\\s*#\\s*region\\b\\)\\|{" ; regexp for start block DID NOT WORK
"\\(^[ \\t]*#[ \\t]*region\\b\\)\\|{" ; regexp for start block
; "\\(^\\s*#\\s*endregion\\b\\)\\|}" ; regexp for end block NO WORKY!
"\\(^[ \\t]*#[ \\t]*endregion\\b\\)\\|}" ; regexp for end block
"/[*/]" ; regexp for comment start
php-hs-forward-sexp ; hs-forward-sexp-func
hs-c-like-adjust-block-beginning ; c-like adjust (1 char)
;php-hs-adjust-block-beginning ; php adjust ?
)
hs-special-modes-alist)
)
;;
;; To use this, put this into your php-mode-hook:
;;
; for hide/show support
(add-hook 'php-mode-hook 'php-region-mode-stuff)
(defun php-region-mode-stuff ()
(hs-minor-mode 1)
(setq hs-isearch-open t)
; with point inside the block, use these keys to hide/show
(local-set-key "\C-c>" 'hs-hide-block)
(local-set-key "\C-c<" 'hs-show-block)
)
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.
In Sublime Text there's a package called SyntaxFold.
Ensure that it has a default with the strings you want (for some odd reason mine had a quote before the hash: '#Start
). I ended up changing it to:
{
//you can change in here other config options
"default":
{
"endMarker": "#EOB",
"name": "Default",
"startMarker": "#BOB"
}
}
You can also alter the key bindings (the ones below in the Usage section) on Windows by changing them in the file:
C:\Users\YOUR_USER\AppData\Roaming\Sublime Text 3\Packages\SyntaxFold\Default (Windows).sublime-keymap
Add the enclosing tags you defined in the config file (you can add a description after each tag), in my case:
#BOB pandora box
public function alpha()
{
# code...
}
public function omega()
{
# code...
}
#EOB pandora box
If you are just interested in folding comment blocks then with Fold Comments package you can press Ctrl+Shift+C to hide and show all comments (it may hide tags of SyntaxFold though).