WTF PHP?

PHP has come a long way in recent years, and though it’s still not by far the most elegant language ever PHP5.3+ at least looks like an actual programming language.

But today I got bitten by something that totally flabbergasted me. Consider the following code:


<?php

$foo = new Foo;

Obviously, without any autoloading mechanism this gives a fatal error as class Foo is undefined. Now, modern versions of PHP have a handy feature where you can call ClassName::class and get the actual classname. This is handy for when it’s imported from some long namespace like so:


<?php

use Foo\Bar\Baz\Fizz\Buzz;

echo Buzz::class; // 'Foo\Bar\Baz\Fizz\Buzz'

So what do you expect happens when you do this to a non-existing class? After all, ::class is some sort of virtual constant on an actual class definition, right?

Wrong.

<?php

echo Foo::class; // 'Foo'
$foo = new Foo; // fatal error, class does not exist

Ehm…  That’s just… Wow.

The only reason I can think of why this works as it does is that the ::class is evaluated at compile time, while the actual instantiation is triggered at run time (thus causing autoloading to kick in).

To add to the party, I ran into this doing a Laravel project, and the autoloader swallows errors in the autoloaded class and simply says “sorry, no such class” (there was actually a typo in the loaded class, which I’d love to have been notified about before wasting an hour). I still need to find out what exactly is going on there, but I’m pretty sure it’s not Composer – I’ve never run into this behaviour on non-Laravel projects. (Also, the error occured in a Laravel “Command” which eagerly swallows errors too, because fuck you programmers debugging stuff.)

Tl;dr: just complain loudly if stuff fails. It’s fine. If I want to handle live errors more gracefully, I’ll just try/catch index.php or whatever myself based on some environment variable thank you very much.

Posted: Modified: Tags:

0 comments:


  1. Насоc скважинный “Vodotok” НПЦВ-ГВ-25-200 – Данный погружной центробежный насос предназначен для перекачивания чистой и слабозагрязненной воды (с процентным содержанием механических примесей до 3%!) из скважин диаметром более 140 мм (!), колодцев, резервуаров, для использования в домашнем хозяйстве и производственных целях, гражданских и индустриальных областях, садоводстве, поливе и т. д. https://7filtrov.shop – https://7filtrov.shop/upload/iblock/3bc/3bcf4d5a5c49de55aed9a58d772c5641.jpg – https:/7filtrov.shop/catalog/nasosy/nasosy_dlya_skvazhin/2085/ – Насоc скважинный “Vodotok” НПЦВ-ГВ-25-200
    Насосы здесь – Насосы на выбор: http://7filtrov.shop/catalog/nasosy/nasosy_dlya_skvazhin/ – купить насос для скважин
    http://7filtrov.shop/catalog/nasosy/nasosy_dlya_skvazhin/nasosy_dlya_skvazhin_15_metrov/ – насосы для скважин 15 метров москва

    http://7filtrov.shop/catalog/nasosy/nasosy_dlya_skvazhin/nasosy_dlya_skvazhin_30_metrov/ – насосы для скважин 30 метров москва
    http://7filtrov.shop/catalog/nasosy/nasosy_dlya_skvazhin/nasosy_dlya_skvazhin_40_metrov/ – насосы для скважин 40 метров

    http://7filtrov.shop/catalog/nasosy/nasosy_dlya_skvazhin/nasosy_dlya_skvazhin_70_metrov/ – купить насос для скважин 70 метров

    http://7filtrov.shop/catalog/nasosy/nasosy_dlya_skvazhin/nasosy_dlya_skvazhin_90_metrov/ – насосы для скважин 90 метров
    http://7filtrov.shop/catalog/nasosy/nasosy_dlya_skvazhin/nasosy_dlya_skvazhin_100_metrov/ – насосы для скважин 100 метров
    http://7filtrov.shop/catalog/nasosy/nasosy_dlya_skvazhin/nasosy_dlya_skvazhin_120_metrov/ – насосы для скважин 120 метров москва

    http://7filtrov.shop/catalog/nasosy/nasosy_dlya_skvazhin/nasosy_dlya_skvazhin_ibo/ – купить насос для скважин ibo

    http://7filtrov.shop/catalog/nasosy/nasosy_dlya_skvazhin/nasosy_dlya_skvazhin_vodotok/ – насосы для скважин vodotok москва.

Leave a Reply

Your email address will not be published. Required fields are marked *