Jay Taylor | programmer notes

TAG | coding

Found myself sort of longing to un-hungarianize some PHP classes.  Here is more information regarding Hungarian Notation.

First off, vim rocks.

Here is the regex to (pretty safely and reliably) de-hungarianify a PHP class:

(Each group is colored for clarity in identifying which section is which in the explanation)

%s/\$[a-z]\([A-Z]\)\([a-zA-Z]\+\)/$\l\1\2/ge | %s/\(private\|protected\|public\|var\)\s\+\(\$_\?\)[a-z]\([A-Z][a-zA-Z]\+\)/\1 \2\l\3/ge | %s/\(\$this->_\?\)[a-z]\([A-Z][a-zA-Z]\+\)\([^(]\)/\1\l\2\3/ge

Explanation:

%s/\$[a-z]\([A-Z]\)\([a-zA-Z]\+\)/$\l\1\2/ge

Does simple variable name transformation of $bSomeFoo into $someFoo. This also removes the hungarian notation from statically referenced variables.

%s/\(private\|protected\|public\|var\)\s\+\(\$_\?\)[a-z]\([A-Z][a-zA-Z]\+\)/\1 \2\l\3/ge

Strips the hungarian notation from class member variable declaractions, e.g. private $_bSomeOtherFoo into private $_someOtherFoo.

%s/\(\$this->_\?\)[a-z]\([A-Z][a-zA-Z]\+\)\([^(]\)/\1\l\2\3/ge

Strips the hungarian notation from class member variable references, e.g. $this->_bSomeOtherFoo into $this->_someOtherFoo.

Keep in mind that this could still break stuff if public class member variables are referenced externally in another file, or if the file you run this on references hungarian-notation in other classes that you haven’t updated.

, , , , , , , Hide

Find it!