Domanda

Why there is the function spl_autoload_unregister ? If I register autoloader in what cases I would want to unregister it ? seems redundant to me.

And another question in that topic: since now php 5.5 comes with built-in opcache, and in the past many install apc - is there any reason to use autoloader all together ? since all the code now will go to the memory anyhow - isn't it better to just make one file which loads all my php classes ?

È stato utile?

Soluzione

You can specify more than one autoload method. So if you have an application that has a lot of autoload methods, it's possible you would want to unregister those methods as well. In practice, this method likely exists more for completeness (not too many projects use so many methods that you would want to unregister an autoload method).

Opcode caching is a different topic. This has nothing to do with autoloading. When PHP is told to execute a file it first parses the file and builds machine level instruction code (operations code, or opcode). The second pass executes the machine level code. Opcode caching (APC, Zend Opcache, etc) simply stores the machine level code so you only need to execute it the next time you call that page, and thus save yourself the extra processing.

Expanded for the comment

You can include all the files if you want, but autoloading does two important things

  1. You can structure your classes within a namespace and use files and directories to mirror the namespace structure. This makes working with your classes very easy because you can tell quickly where each class file lives
  2. You're including files only as you need them

As for your opcache, your thinking is incorrect. Let's say you include all classes and methods you use. Now, let's say you have a page that only uses 25% of your code base. That means you loaded the other 75% and forced the opcache to cache it. But to what end? Opcache works on a file-by-file basis, not a project level basis. So you would be bloating your code on every page for no gain because autoloading would have included the code you needed anyways, but dynamically.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top