Skip to content

Instantly share code, notes, and snippets.

@sirreal
Created February 6, 2024 11:53
Show Gist options
  • Save sirreal/3d4174b57df87b972e4705705024d672 to your computer and use it in GitHub Desktop.
Save sirreal/3d4174b57df87b972e4705705024d672 to your computer and use it in GitHub Desktop.
Demo php clase name collision problems
<?php
echo "Running test from: " . PHP_VERSION . PHP_EOL;
// Comment this line to trigger other errors
require_once __DIR__ . '/unconditional.php';
require_once __DIR__ . '/b.php';
require_once __DIR__ . '/c.php';
$a = new Already_Defined();
// Uncommenting the top require_once would cause an error here
require_once __DIR__ . '/unconditional.php';
try {
// Uncommenting this line would cause fatal errors
// class Already_Defined {};
// Uncommenting the top require_once would cause an error here
require_once __DIR__ . '/unconditional.php';
} catch ( Error $e ) {
echo "Creating the class unconditionally would error." . PHP_EOL;
var_dump($e);
}
echo "Ran to completion, created class instance: " . get_class($a) . PHP_EOL;
<?php
if (class_exists('Already_Defined') ) {
return;
}
class Already_Defined {
public function __construct() {
echo 'Initialized from ' . __FILE__ . PHP_EOL;
}
}
<?php
if (class_exists('Already_Defined') ) {
return;
}
class Already_Defined {
public function __construct() {
echo 'Initialized from ' . __FILE__ . PHP_EOL;
}
}
FROM php:7.0-alpine
COPY . .
ENTRYPOINT ["php", "a.php"]
FROM php:8.3-alpine
COPY . .
ENTRYPOINT ["php", "a.php"]
docker build --quiet --file ./Dockerfile.php7 --tag demo-php-70 .
docker build --quiet --file ./Dockerfile.php8 --tag demo-php-83 .
docker run demo-php-70 || echo "Errored."
echo "---"
docker run demo-php-83 || echo "Errored."
<?php
class Already_Defined {
public function __construct() {
echo 'Initialized from ' . __FILE__ . PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment