简体   繁体   中英

PHP 4 class autoloader

I'm working in PHP 4 with classes but there's not __autoload function, so I have problems to load my classes because they are intertwined.

I have a class Ship and a class Movement. The class Ship contains an Movement object and the class Movement contains an Ship object.

So when I do the require Ship, the class is read and throws error when reach the new Movement and conversely.

Some solution? ;)

PHP 4 is really, really old and not-supported. The best option is to move to PHP 5. If you can't, create a bootstrap file, which requires all class definitions (in the correct order in case of inheritance); make sure the class definition files contains only class definition (and not executable code like $obj = new Movement) and require this file in each file you are actually running in your application.

The point is, the class definition of Movement is not needed before the new Movement statement, and if this statement is in some Ship's method (even if it's in the constructor), you can safely load the Ship.php, then Movement.php, then run the code and it will work.

Also, make sure to load all class definitions before starting the session, if you are using sessions and serialize objects in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM