Sorting functions in source code alphabetically

I have a PHP class with a bunch of methods. They are currently in no particular order. I would (at least initially) want them to be in alphabetical order, so there public function a()

will be sooner public function b()

, etc.

I'm sure I can write a script, but is there an existing program that can do this for me? All I can find are ways to sort individual lines.


source to share

2 answers

You can find Reflection documentation , here is an example:

$reflector = new ReflectionClass('example');
$methods = $reflector->getMethods();
usort($methods, function($method1, $method2) {
    return strcasecmp($method1->getName(), $method2->getName());


However, I would not recommend that you do this, because it is better to order your methods according to what they do for you or any other programmer who is most likely to read your code.

What you want to do is work with an IDE that can change the view without changing the source code. Many IDEs can do this, including Netbeans and Eclipse.



Various IDEs can do this for you. Obviously Eclipse can, I know PHPStorm can and Netbeans can do that too.

But I wouldn't do that. It is not necessary. The correct IDE should be able to display the list of methods in any order and you can navigate to them.

Also, if you want to rename a method, you will have to move it as well. If you are making changes to version control, it looks like the entire method has been removed and another has been inserted. It really messed up your story and made it difficult to see changes.

We had alphabetical order as a coding style guide before, but it fell for this reason (thankfully).



All Articles