composer / composer

Dependency Manager for PHP

Home Page:https://getcomposer.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

in swoole projects, class_exists() causes "include" the same class more than once

yespire opened this issue · comments

In swoole projects, when using class_exists(ClassName), the following line will try to include class file everytime when class_exists() is run:

https://github.com/composer/composer/blob/main/src/Composer/Autoload/ClassLoader.php#L576

  • Question: is there a reason to prefer "include" over "include_once" ? Is there a downside of using "include_once" here ?

Last time I checked include works faster than include_once as it does less checks, and it seems to works for everything not messing with the process at least. I'm not sure what the actual cause of the issue with Swoole is, but I'm also pretty sure others have successfully worked with class_exists AND swoole, so maybe it's something else.

@Seldaek

I found the issue -- extra "\" in the classpath

In case you want to replicate this issue:

// require "vendor/autoload.php";
// assume class "Lib\Arr" exists
class_exists('Lib\Arr'); 
class_exists('Lib\\\Arr');   // notice 3 slash --- this will throw exception about "PHP Fatal error:  Cannot declare class Lib\Arr, because the name is already in use"

In classLoader 'Lib\Arr' and 'Lib\\Arr' are treated as 2 separate keys, but they resolve to the same php file, hence the double inclusion.

Suggestion: it would be nice if ClassLoader can throw an exception on malformed classpath, ie: "Exception: malformed classpath Lib\\Arr"

Yeah no sorry this is the same issue as class_exists('\\'), you have to ensure this is called correctly, or if anything PHP should error out. It's not our job to validate this and error IMO, and it would just slow down every class loading call in every project using Composer which is a lot of wasted CPU cycles for a rare mistake.

But glad you found out the problem..