symfony / polyfill

PHP polyfills

Home Page:https://symfony.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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):

if (!function_exists('normalizer_is_normalized')) {
function normalizer_is_normalized($s, $form = p\Normalizer::NFC) { return p\Normalizer::isNormalized($s, $form); }
function normalizer_normalize($s, $form = p\Normalizer::NFC) { return p\Normalizer::normalize($s, $form); }
}

Unfortunately, this is not done in Normalizer.php:

<?php
class Normalizer extends Symfony\Polyfill\Intl\Normalizer\Normalizer

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...