Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 5.4 vs 5.3: Autoloading multiple classes in the same file

Tags:

php

Firstly, this is a bit of a long one, so thank you for reading.

My issue is similar to this one: Class not found in the same file

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.

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.

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.

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

  1. A static method gets called in PublicCustomer.

  2. The autoloader finds and loads the correct file.

  3. 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).

  4. The method in PublicCustomer then tries to call a method in PublicCustomerDBReader. This fails, since our autoloader has already required the file once.

It seems to me that I have two solutions:

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

Have I understood the issue above properly?

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

Are there any other potential solutions to the problem that I have not considered?

like image 774
user2353938 Avatar asked May 06 '13 09:05

user2353938


People also ask

Can you have multiple classes in a PHP file?

The only time I think it's correct to use more than one class per file is when you are using internal classes... but internal classes are inside another class, and thus can be left inside the same file. The inner classes roles are strongly related to the outer classes, so placing them in the same file is fine.

Can multiple classes be in the same file?

No, while defining multiple classes in a single Java file you need to make sure that only one class among them is public. If you have more than one public classes a single file a compile-time error will be generated.


1 Answers

Place the reader/writer classes at the head of the file. Also you might consider filing a bug report, since the parser should only halt on errors.

like image 187
y o Avatar answered Nov 10 '22 02:11

y o