I wrote a concise guide to OOP in PHP that walks you through classes and
objects, inheritance, interfaces, all the way to exceptions. No
philosophizing – just a factually correct foundation you can build on. For
instance, right away in Nette.
PHP 7 tamed fatal errors into exceptions, but two hundred compile-time errors
act as if it's still 2004. There's no way to catch them,
php_check_syntax() was taken away from us after four minor
versions, and the only option left is to call an external linter. I show how to
do it – and what PHP 8.3 promises.
The ?? operator took 12 years to be born, arriving at the exact
moment PHP stopped needing it. Instead of simplifying life, it now reliably
conceals typos and bugs. I would have preferred if we had waited even longer
for it.
The tabs vs. spaces debate has a thousand arguments on both sides, but one
knocks them all to their knees: accessibility. A blind programmer on a 40-cell
braille display wastes a quarter of the display because of your four spaces.
Tabs aren't a preference – they're consideration.
Texy 3.1.6 introduces the {texy} tag for Latte 3 – three lines of
configuration and you're writing directly in Texy syntax in your templates.
Static content is translated at compile time, dynamic content at runtime. I'll
also show a trick with a custom callback function and passing parameters.
For five years, I was afraid to take the plunge. A completely rewritten
compiler, a custom PHP parser, an AST tree – not a single line of the
original code remains. Latte now has the internals I've been dreaming about for
eleven years.
For twenty years I dutifully wrote static on constant properties
and SCREAMED IN CAPS for constants, because “that's how it's done.” One
stopped making sense sometime around PHP 7.3, the other never made sense in PHP
at all. The bigger the nonsense, the deeper its roots.
For years I refused to write the question mark for nullable types, because
redundancy physically hurts me. Then PHP 8.0 came along with promoted
properties and consistency forced me to capitulate. A story about how your own
principles catch up with you.
How to objectively compare framework documentation? I measured the
documentation-to-code ratio for seven major players. The result is surprisingly
even – and that's actually the most interesting takeaway. Nette is above
average and is the only one with a complete Czech translation.
Shutdown in PHP has its own ritual, and destructors play a role in it that few
people understand correctly. Global objects are torn down in reverse order by a
clever algorithm, while the rest only get a courtesy __destruct
without actually being destroyed. And the best part saved for last: calling the
destructor is not guaranteed at all.