PHP 5.4 vs 5.3: Autoloading multiple classes in the same file
<p>Firstly, this is a bit of a long one, so thank you for reading.</p>

<p>My issue is similar to this one:
<a href="http://stackoverflow.com/questions/12675835/class-not-found-in-the-same-file">Class not found in the same file</a></p>

<p>I have a custom-built framework originally written in 2008 for PHP 5 and it's been upgraded over the years to work with PHP 5.3. I've been looking at 5.4 compatibility and have hit a serious issue.</p>

<p>The ORM layer automatically generates classes for each DB table. These classes all sit in one file per table and our autoloader loads that file when required. 
For example, a 'customer' table in the 'public' schema (postgresql) would have the following classes:
PublicCustomer, PublicCustomerDBReader, PublicCustomerDBWriter.
Now this may not be the ideal set up, but it is what we currently have.</p>

<p>In PHP 5.3, if PublicCustomer was required, the file would be included, parsed and all of the above classes would become available. So if, for example, a static method is called on PublicCustomer, and that method calls something in PublicCustomerDBReader, that would work fine, since that class is in the same file.</p>

<p>In PHP 5.4, it looks like some optimisations have been done in the core. In the above scenario:</p>

<ol>
<li><p>A static method gets called in PublicCustomer. </p></li>
<li><p>The autoloader finds and loads the correct file.</p></li>
<li><p>The PHP parser only parses up to where it needs; the PublicCustomer class. It has not parsed or instantiated the PublicCustomerDBReader class. I can confirm this by testing if the class exists and by seeing if the parser reaches the end of the file when it gets included, when the method is called (it doesn't).</p></li>
<li>The method in PublicCustomer then tries to call a method in PublicCustomerDBReader. This fails, since our autoloader has already required the file once.</li>
</ol>

<p>It seems to me that I have two solutions:</p>

<ol>
<li>Separate these classes out so that there is one file for each (this will produce a huge number of files)</li>
<li>Redesign the ORM layer so that multiple classes are not required.</li>
</ol>

<p>Have I understood the issue above properly? </p>

<p>Does anyone know if an optimisation or change was made in PHP 5.4 that would cause this behaviour?</p>

<p>Are there any other potential solutions to the problem that I have not considered?</p>
