php - Autoloader实现,预加载所有类

I'm new to php and inherited a website project with hundreds of pages, all procedural (when I do a text search of the files, there isn't even a function definition anywhere). I'm coming from the c# and Java worlds. I'm looking for a way to incrementally add OOP. (They want me to update the front end and I am trying to convince them of fixing the backend at the same time and they don't want to use a framework (dammit)).

While looking into autoloader... Well, here's my understanding. It's a method of registering folders where classes are stored and when you instantiate a class, trait, etc. it searches the folder based on the class/filename/namespace and loads the appropriate definitions.

I have a few questions:

Does autoloader search the folder and load the appropriate definitions on every page lifecycle (or does it cache them)?

Pre-loading: Is there a way to use autoloader, or some alternative, to pre-load ALL class definitions into memory and make them available across all sessions?

If so, when updating class files, how would I tell this mechanism to reload everything to memory when I make changes to class files?

UPDATE TO QUESTIONS:

Thank you both for your answers and it helps a little, but... I do have a bad habit of posing the wrong question(s) on StackOverflow.

The thing I want to avoid is slowing down pages by adding classes. So let's say I add a library and register the paths with autoloader. A page instanciates a class with multiple dependencies. Let's say that the dependency graph includes 15 files. For each request lifecycle, the server loads the page and 15 other files just on that one page.

Since I am coming from compiled languages, I feel a little strange not loading these classes into memory. All the classes together should not be over say 5MB.

(Or maybe I should just create a RAM Disk and copy all the files in there on boot and just have a symlink?)

No one posted what I was looking for but it seems the best route is the OptCache that's prebuilt into php 5.5 and above (my client is using 5.3 so I didn't know about it).

https://github.com/zendtech/ZendOptimizerPlus

The Zend OPcache

The Zend OPcache provides faster PHP execution through opcode caching and optimization. It improves PHP performance by storing precompiled script bytecode in the shared memory. This eliminates the stages of reading code from the disk and compiling it on future access. In addition, it applies a few bytecode optimization patterns that make code execution faster.

I think you may misunderstand the purpose of autoloading. It is simply instructions on what to do when your code calls for a class that PHP doesn't recognize. That's it. The autoloader just calls requires /path/to/classfile so that PHP will see the class.

Does autoloader search the folder and load the appropriate definitions on every page lifecycle (or does it cache them)?

There is no caching across requests, so if you make a change to file, the next http request will incorporate those changes. It's just as if you changed any other instruction in your script, for example change echo 1 to echo 2

Pre-loading: Is there a way to use autoloader, or some alternative, to pre-load ALL class definitions into memory and make them available across all sessions?

There is no need for this. A well written autoloader has instructions for where to find any class, so loading all possible classes ahead of time is wasteful. If you're still running into undefined classes errors, you need to either improve the autoloader or place the class files in accordance with the current autoloader instructions.

If you really want to preload all your classes, use the auto_prepend_file setting in php.ini. The docs say

Specifies the name of a file that is automatically parsed before the main file

Set it to an initialization script. In that script have something like:

//put all your class files in this folder
$dir = '/path/to/classes/folder';
$handle = opendir($dir);

//require all PHP files from classes folder
while (false !== ($item = readdir($handle))){
    $path = $dir.'/'.$item;
    if(is_file($path) && pathinfo($path,PATHINFO_EXTENSION)==='php')
        require_once $path;
}

This is simplified. There is significant risk in just including all files in any directory into your script so I would not do this. You would also need to adjust this if you want to include files in subdirectories.

Basically, don't do this. Just have a good autoloader.

Auto loaders in PHP are lazy. When PHP encounters a the use of a class it doesn't know about, it will ask the registered autoloader (or chain of autoloaders) to go find it. It's the autoloader's job to figure out where to get the file the class is defined in and include it. Having some sort of convention for naming your classes and organizing your class files is key to having a useful autoloader, and several conventions have arisen in the PHP community, such as PSR-4.

Does autoloader search the folder and load the appropriate definitions on every page lifecycle (or does it cache them)?

The autoloader(s) is(are) called on every request, but only when the need to autoload a class arises.

Pre-loading: Is there a way to use autoloader, or some alternative, to pre-load ALL class definitions into memory and make them available across all sessions?

I don't believe so, but as the number of classes grow, this becomes more and more wasteful.

Welcome to the wonderful[citation needed] world of legacy PHP, I highly recommend you check out Modernizing Legacy Applications In PHP. It's like a strategy guide for getting from Mordor back to the Shire.