简体   繁体   中英

Composer - autoload class on specific PHP version

We are actually migrating our PHP project from PHP 5.3 to 7.2.

I recently migrate many libraries to Composer.

I have a problem to replace PHPExcel with PhpSpreadSheet which doesn't support PHP 5.3.

I keep PHPExcel in a separate folder for the moment and I use PHP_VERSION_ID to use either one or the other.

// early in the file to use another dependency
require_once 'vendor/autoload.php';
.
.
.
if (PHP_VERSION_ID > 50400) {
      $workbook = new PhpOffice\PhpSpreadsheet\Spreadsheet();
} else {
      require_once 'lib/PHPExcel/Classes/PHPExcel.php';
      $workbook = new PHPExcel();
}

For the moment, our code must keep running under PHP 5.3 and 7.2.

Is there a solution to tell Composer autoloader to not autoload PhpSpreadSheet under PHP 5.3?

Is there a solution to tell Composer autoloader to not autoload PhpSpreadSheet under PHP 5.3?

Composer's autoloader will not load any class as long you're not request it. So the simplest option to tell Composer to not loading specified class, is to not using it. :)

However migrating from PHP 5.3 to 7.2 is a big task, trying to make your app compatible with both these version may make it really hard. I suggest to treat this as two separate versions developed on different branches with different PHP version required. It should be easier to have two lines where one should care only about PHP 5.3 and second only about PHP 7.2, than hacking one line to be compatible with both PHP 5.3 and 7.2. PHP 5.3 was released in 2009 - its ~7 years older than PHP 7.2, and many things was changed at this time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM