DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > The Trouble With Two Autoloaders

The Trouble With Two Autoloaders

Unfortunately, unlike belts and suspenders, two PHP autoloaders are not twice as good at holding your pants up. It goes beyond simple redundancy; having multiple autoloaders can actually be actively harmful to your application. Read on and find out why.

Greg Anderson user avatar by
Greg Anderson
·
Mar. 30, 16 · Web Dev Zone · Analysis
Like (1)
Save
Tweet
4.54K Views

Join the DZone community and get the full member experience.

Join For Free

An autoloader is a bit of PHP code designed to load class files as they are needed. This is a language feature of PHP; when an autoloader is registered, PHP will call it any time a reference to an unknown (un-loaded) class is made. Composer makes it even more convenient to use an autoloader, as it will generate one automatically from the information provided in a project’s various composer.json files when the project is installed and updated.

If one autoloader is good, what about using two autoloaders? Composer has been specifically designed to make it possible to include more than one autoload file; in fact, in the early days of Composer, this was the sanctioned way for the unit tests to combine the classes needed for testing with the classes from the application. Unfortunately, unlike belts and suspenders, two PHP autoloaders are not twice as good at holding your pants up. It goes beyond simple redundancy; having multiple autoloaders can actually be actively harmful to your application.

The figure below illustrates how things might go wrong.

Image title

Figure: Loading Two Different Copies of the Same Library

In this hypothetical scenario, one program has a library, FancyLib v1.0.1, loaded in its autoload file. If this program then loads a second autoload file that also contains the same library, but at a slightly different version, problems are likely to be encountered. There are a number of ways that things can go wrong; in the example above, a protected function in a base class has been renamed. If a subclass from the second library is loaded and used with the base class from the first library, then a fatal error will be thrown at runtime. The public API for the library has not changed, so Semantic Versioning provides no protection.

Fortunately, it is unusual to encounter a situation where it is tempting to use more than one autoloader. Perhaps the most common example of where two autoloaders are used is when using a global install of Drush or Drupal Console to run commands on a Drupal 8 site. Keeping this working gets a little trickier every time Drupal 8 updates its composer.lock file. At the moment, it is still possible to use the same Drush and Drupal Console with both Drupal 8.0.x and Drupal 8.1.x, but this situation can change with any future release. Stay tuned for future improvements in this area; in the meantime, you might want to consider using a site-local Drush to guard against future problems.

unit test Drupal Library PHP application Composer (software) Console (video game CLI) Moment

Published at DZone with permission of Greg Anderson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Creating a REST Web Service With Java and Spring (Part 1)
  • Autowiring in Spring
  • Challenges to Designing Data Pipelines at Scale
  • Why Is Software Integration Important for Business?

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo