Subjective question: Should you create class aliases in your bootstrap file for your custom classes?

Just getting back into playing around with Modx again :slight_smile:. And perhaps work on some updates, enhancements, for ExtraBuilder.

This specific question is a clarification on custom tables/models when calling $modx->newObject("myClass"). Is there a way to make this work with the short class name? Or on MODX v3 you just need to use the full class?

I think I just answered my own question :slight_smile: and found this documenation:
Changed Class Names - Upgrading from 2.x to 3.0 | MODX Documentation

The aliases are loaded in the /core/include/deprecated.php file. So let me change my question to a more subjective one.

The class aliases allow you to use either $modx->newObject("modResource") OR $modx->newObject("\MODX\Revolution\modResource").

  1. Is it a bad idea to include your own class aliases in your bootstrap.php file?
  2. Or should we just get used to the full class names/paths format?

I kind of like the idea of including it in my bootstrap file so that I can use the shorthand values to access the classes.

1 Like

In my view it’s a bad idea.
It defeats the purpose of having namespaces (which is avoiding naming conflicts), if class aliases are created for every custom class.

To avoid having to write the full class name all the time, it’s better (in my opinion) to use a use-statement at the top:

use MODX\Revolution\modResource;
...
$modx->getObject(modResource::class, ...);

Ah, right. Thanks for the reminder about “use” statements.

So, the bootstrap “use” statement doesn’t carry-over to the Snippet processing? It seems you have to put “use” statements at the top of your Snippet for any classes you want to use.

I guess that’s partially the point so that you know for that specific PHP what classes are being used?

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.