|
It's weird for me. I used to write both Java and PHP in the past. Custom PHP framework we built took into account the way PHP is executed: you are stateless and need to setup everything on every request (runtime is fast to start with FPM and opcode cache). Namespaces based cheap autoloader worked great. No composer. We used singletons for getting the configuration and connections to DBs. There was almost no setup code that needed to be run every time other than loading .ini based configuration and connecting to the DB. Our webapp responded under 20ms (DB and Sphinx included), and I could get it to respond in 1 ms for things where we needed to be quick and didn't have to output HTML with Forms. I was really small and you could read the whole framework code in 1-2 hours. It worked with SQL in a reasonable way. You didn't have to write your SQL for simple CRUD, but for larger things involving joining multiple tables and more complex expressions we wrote native SQL. I switched jobs, and started with Symfony 3. The thing felt like some Java framework, but poorly documented and harder to use than it should be. It had lots and lots of setup code done before handling every request. There's a whole DI framework with it's own tons of setup code for every component. And you have to do setup even though you don't use the component in that particular request. There's ways of doing lazy setup, but you still waste time to wire that up. Framework overhead can be 30-100ms. Other modern PHP frameworks have similar overhead. I know that there's PHP-PM, to save some of that work that isn't really $_REQUEST specific. Everybody is using type hints wherever they can, and it feels as verbose as Java, but without compile time type safety, and you can't really put type information everywhere (class members for example).
Even though PHP runtime has made great progress (it's probably the fastest interpreted language, and it's great it has reference counted garbage collection), it feels like language is struggling to find it's identity, with it taking a lot from Java and still coping with ugly legacy ($, having to use $this inside a class function, php.ini, features for supporting templating even though it's rarely used as a templating lang in modern frameworks). Doctrine and it's verbosity feels very ugly to me. Learning Python and Flask (as an example) was much more enjoyable than switching from a nimble custom PHP framework to Symfony. I'd argue you can write fast, simple and maintainable PHP, using state PHP runtime has setup for you ($_POST, $_GET, $_SERVER etc), namespaces and a namespace based autoloader, trying to use pure functions when you can (using static classes shouldn't be a sin - just to split your code in sensible parts), and using general good practices for writing readable and maintainable code (avoid long functions, huge classes, too much block nesting, decoupling, naming things in a good way). With some conventions you can write a decent and productive framework quickly, but you could do that with a nicer language too, so what's the point? |
This is also the overarching feeling I get from the big PHP frameworks: Java envy. They picked an architecture which is perfectly sensible in Java, but slow by design in PHP. You also notice this in the language features being added, where gradually PHP is turning into Java. On the Java side instead there is Scala envy, with Java slowly turning into Scala.
I don't mind PHP taking good ideas from Java though, PHP 7.x is much improved and the community overall seems to be moving in the right direction: take the good ideas from Java, without taking on the worst excesses.