0

I come from Java/Netbeans, and I can indent my Java code using alt+shift+f, I have searched through this site and other articles, but I didn't find a good way to indent PHP.

What I found:

  1. using pear PHP_beautifier

but I have to output the result to a different file or simply copy on terminal result.

  1. using PHP beautifier on the web

I have tried using M-x indent-region in PHP-mode but nothing changes. I tried in jdee for Java code M-x indent-region also doesn't work.

I found other solutions but for JSON, com.lang, Asytle (C/C++/C#)

this is my .emacs

https://gist.github.com/azzamsa/1f76b8ca00fd2737ae08a5c3f6dc6b3f

I am using :

php-mode-1.18.2(ejmr/php-mode), GNU Emacs 24.4.1

and installed php-mode from elpa repo.

After following phils suggestion to use Tab, it works. But when I try to break the code format, and try to see the result using C-x+ h and Tab, it doesn't indent the code properly as what online formater does. e.g.:

    public function

        __construct($name) {
        $this->name = $name;
        //$this->numLeg = $numLeg;
    }

    public

        function

            bark() {
            return "Woof!";
        }
    public

        function

            greet() {
            return "hello i am " . "$this->name";
        }


}

There is no result even when I use indent-region. I also tried to use fill-region, and other choice of fill, but no luck. But the online formatter can handle this nicely, the result from online formatter and the expected one is:

class Dog {
    public $numLegs = 4;
    public $name;

    public function __construct($name) {
        $this->name = $name;
        //$this->numLeg = $numLeg;
    }

    public function bark() {
        return "Woof!";
    }
    public function greet() {
        return "hello i am " . "$this->name";
    }


}
$dog1 = new Dog("Barker");
$dog2 = new DOg("Amigo");
  • indent-region (usually invoked via TAB) is the correct answer, so you should show an example of what isn't working for you (with the original formatting, and the formatting you are expecting to see after reindenting). – phils Mar 28 '17 at 6:49
  • Also say which version of Emacs you are using, and which php-mode (where you got it from, as well as version) you are using. – phils Mar 28 '17 at 6:51
  • (After reading the updated question): indent-region deals only with indentation, not joining statements which are (bizarrely) split over multiple lines into single lines. It doesn't reformat your code; it just sets the indent of each line. In other words, what you're asking for isn't "how to indent PHP code". I don't think php-mode can help you, so you might as well continue to use the other tools you've found. – phils Mar 28 '17 at 10:14
  • thankfully now i can continue to learn php in emacs after your answer. thanks lot for your great answer and suggestion, really thanks. may i know what tool you use for reformating PHP or Java code in emacs ?. – azzamsa Mar 28 '17 at 12:36
  • I've rarely had a need for anything like that. I format my own code the way it ought to be formatted (with Emacs indenting), and I'm unlikely to want to introduce version-control noise by reformatting code I'm not working on, so I generally don't do that (and coding standards make that an unusual need in any case). The small amount of reformatting I do is manual. I'll take a look at php-beautifier.el though. – phils Mar 28 '17 at 20:41
1

This problem is solved.

Thanks to Phil Newton (sodaware) for creating this extension to integrate with PHP_beautifier.

This application was released 5 hours ago (on March 28, 2017).

I have tried some of my PHP Code, and it works well.

This is some gif from when I gave it a try.

php_beautifier_el_1

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.