class_exists for Normalizer
matzeeable opened this issue · comments
Hi!
I want to use polyfill-intl-normalizer
in a WordPress plugin. Unfortunately I get errors from users which have already installed ext-intl
:
PHP Fatal error: Cannot declare class Normalizer, because the name is already in use in /home/mg/vscode-workspace/[...] on line 3
In a JavaScript world a polyfill is always loaded but only created, if the functionality is missing.
In your composer.json
you are defining autoloading files: Normalizer.php
and bootstrap.php
. In your bootstrap.php
you are checking if a function exists (that's correct in a polyfilled-world):
polyfill/src/Intl/Normalizer/bootstrap.php
Lines 14 to 17 in 2b84d9e
Unfortunately, this is not done in Normalizer.php
:
polyfill/src/Intl/Normalizer/Resources/stubs/Normalizer.php
Lines 1 to 3 in 2b84d9e
Is there any reason that there is no class_exists
check? If not, I can start a PR with the following changes:
<?php
if (!class_exists('Normalizer')) {
class Normalizer extends Symfony\Polyfill\Intl\Normalizer\Normalizer
{
const NONE = 1;
const FORM_D = 2;
const FORM_KD = 3;
const FORM_C = 4;
const FORM_KC = 5;
const NFD = 2;
const NFKD = 3;
const NFC = 4;
const NFKC = 5;
}
}
Regards, Matthew 🙂
There is no class_exists check, because we rely on autoloading to load the file, and the autoloader will only be triggered if the class does not exist.
Also, I think OPCache disables some optimizations when a class definition is conditional, so would be a bad change as there is no issue currently.
Hi @stof !
You are completely right, for some reason a composer dump-autoload
fixed the issue...