by Legion · 13y ago · 0 comments
For non-trivial web applications, the "rendering HTML strings" is one of the smallest parts of the job.
The lack of separation between application and HTML output in PHP may be an asset when starting out or when making lightly scripted web pages, but when creating an application that just happens to be served up over the Web (and possibly served up other ways too), those general-purpose languages are much more natural fits for the job.
If you echo HTML, you get HTML. If you echo binary data, you get binary data. JSON, XML (including SOAP bodies), text, nothing at all, file i/o, direct socket connections, exit codes - all are supported equally well. The only thing vaguely related is the HTTP headers that you get for free if you're running it behind a fastcgi module (apache's mod_php, nginx/php-fpm, etc).
You can write a single PHP file that includes HTML, Javascript, MySQL, CSS and PHP, but that would make you a bad developer, or it's such a simple one-off thing that you don't worry about it.
Or, you can develop with maintainability in mind and separate those 5 concerns out.
Tools facilitate workflows. The fact is, PHP very strongly facilitates your "bad developer" path. That's why there is so much PHP code that goes down that path, compared to other languages.
Yes, you can go off on a better path. But better designed tools make it much more easy and natural to do things the "right" way.
PHP makes your "bad developer" path the path of least resistance. That's bad.
.Net languages aren't immune to this either, particularly Windows.Forms and ASP.net. To write decent code in those, you basically have to fight the IDE and the framework every step of the way. ASP.net MVC makes it much easier to write good code, at the cost of being harder to user if you don't already know what to do. Your average newbie can't just point and click until he's half way there.
PHP has decent frameworks, like Symfony. They make it much easier to do the right thing, and harder to do the wrong thing, at the cost of being more difficult to learn.
Props to the Symfony team though. They're still trying to make something good out of PHP long after most other decent developers abandoned the platform on favour of something less newbie-driven. It helps a lot to have that kind of framework available, just in case I have to use PHP for something.
Edit: Accidentally said that ASP.Net MVC encouraged bad code, but meant it encouraged good code.
Browser -> POST -> Database -> XML -> PHP + XSLT => HTML
Prevents HTML from being mixed with PHP. PHP becomes the "controller" in the MVC world. This makes the application easy to migrate to another language.The GP is using a very well thought out configuration; this is using the right tool for the job incarnate. If I wasn't lacking in SQL department I would probably do exactly the same (probably replacing PHP with something strongly and maybe statically typed).
[1] SQL and XML seems a bit redundant, but I don't know what they are used for exactly. Also I lumped both SQL and XML together as "relational" because of XQuery, but I admit that I don't know much about the latter and this may be not accurate.
Using XML allows means the data associated with every "page" in the system automatically doubles as a service (SaaS). XSLT allows those those services to be translated into JSON, ASCII, PDF (via LaTeX), XML, or the format du jour. With this architecture, the amount of time to publish a new export format for every "page" in the system is reduced to the time it takes to write a new XSL template: hours to a few days.
All pages can reuse the same code to render themselves:
$data = get_xml( $id );
$xml = get_xml_dom( $data );
$xsl = get_xsl_dom( $stylesheet );
$proc = get_xslt_processor( $id, xsl );
echo $proc->transformToXML( $xml );
This cleanly separates business logic and the data model from presentation, plus much more. Perhaps it is masochism, though. ;-)Having to duct-tape XSLT on top of PHP just because PHP can't cut it is rather perverse.
The only thing I can think of that's worse than "programming" in XSLT is ColdFusion.