An organised way to rename classes

An organised way to rename classes

by Sam Hemelryk -
Number of replies: 1

Hi guys || girls,

I'm posting here to get a bit of feedback on an idea I have to bring organisation to the way in which we rename a class for auto-loading while having to support the original class name for backwards compatibility.

MDL-43365 - Create a better method of renaming a class for auto-loading

This proposal uses the existing auto-loading code and cache to intercept requests for a deprecated class that has been renamed. When a request is intercepted the autoloader creates a class alias for the old name to the new name and issues a developer debug call to advise the upgrade of code to use the new name.

Things worth knowing about this proposal:

  • Extends the existing auto-loading system and cache.
  • Renamed classes get registered in a new db/renamedclasses.php file.
  • The old class can be removed entirely, leaving just the registration above.
  • The debugging notice only gets issued once per class. So even if the old class name is used frequently in your code you will only get a single debugging call.
  • By using class_alias the two classes appear identical and things such as type-hinting on the old and new names both continue to work.

In my mind the big advantage to this approach is that we can truly benefit from auto-loading with this method as essentially it is allowing both the new and old classes to be auto-loaded rather than the current approach of making the old class extend the new class which because the old class had to be included manually means we don't actually auto-load the new class. This makes renaming impractical at present. One other advantage is that debugging notice. With this we would have a standard message displayed reliably for all renamed classes.

To see the complete picture in code check out the diff on github.

As proof of concept and as part of the patch I have converted the textlib, collatorlib, plugin_manager, and plugininfo_base deprecated classes. I also imagine this will be used with the upcoming output changes.

Please if you've an opinion or thoughts on this share them either here or on the issue, and feedback is most welcome.

Many thanks Sam

Average of ratings: Useful (4)
In reply to Sam Hemelryk

Re: An organised way to rename classes

by Sam Hemelryk -

Just to summarise, to move a class so that is it auto-loaded and no longer needs to be manually included you would:

  1. Create a new php within the classes directory named as auto-loading requires.
  2. Copy the class code from its current location to the new location.
  3. Delete the class from the old location (do not delete the file).
  4. Creator edit db/renamedclasses.php and add a line for your class mapping its old name to its new name.

Done!

Average of ratings: Useful (3)