Skip to main content

Dynamic Use-Statement Generation

Automatically manage PHP namespace imports with clean, conflict-free use statements.

What Is Dynamic Use-Statement Generation?

In Joomla Component Builder (JCB), Dynamic Use-Statement Generation is the automatic creation of PHP use statements based on the classes, traits, interfaces, or helper functions referenced in your componentโ€™s code. This feature ensures that all required PHP classes are properly imported into your files without duplication, typos, or namespace issues โ€” even across Joomla versions.

Itโ€™s designed to eliminate manual namespace management, streamline dependency injection, and prevent runtime errors caused by missing class references. When you use dynamic class references like Joomla Powers (JPKs), custom helper classes, or Super Power entities, JCB automatically includes the correct use statement โ€” scoped to the correct file.

Key Features

  • ๐Ÿง  Automatic Class Import Detection: JCB scans your layouts, models, controllers, templates, and custom code blocks to detect class references that require importing.
  • ๐Ÿ”— Smart Namespace Resolution: Whether your class is part of Joomla core (e.g., Joomla\CMS\Factory) or a namespaced Super Power you created, JCB resolves the correct namespace and maps it to a proper use statement.
  • ๐Ÿชช Alias Handling: If a conflict is detected or an alias is defined in JCB (e.g., JFactory as LegacyFactory), JCB respects that and generates use statements with as.
  • ๐Ÿ”„ Version-Aware Imports: When using Joomla Powers (JPKs), JCB inserts the correct use statement depending on your Joomla version (J3, J4, J5) โ€” keeping your compiled code version-safe and clean.
  • ๐Ÿ“‹ Scoped Insertion: use statements are inserted at the top of each PHP file (controllers, models, helpers, views), only if needed โ€” and only once per class.
  • ๐Ÿงพ No Duplicates or Dead Code: Redundant imports are skipped, and unused use statements are removed, keeping the output clean and efficient.
  • ๐Ÿงฌ Works with Custom Code: Even if you reference a class manually in a custom code block or Insert Tag, JCB will still scan and insert the appropriate use statement.

Use Cases

  • โœ… Use Joomla\CMS\Router\Route inside a custom controller block โ†’ JCB adds use Joomla\CMS\Router\Route;
  • ๐Ÿง  Reference a Super Power class in your helper โ†’ JCB adds the correct namespace to your helper.php
  • ๐Ÿ”„ Use Joomla Powers like @jimport:JFactory in your logic โ†’ resolved to the correct class with version-aware use statements
  • ๐Ÿงฑ Your layout uses \Joomla\CMS\Language\Text โ†’ JCB includes use statement at the top of the layout file automatically

How to Use

  1. No action is needed from you in most cases โ€” JCB handles this automatically.
  2. If you want to manually trigger or confirm behavior:
    • Include a class reference in your logic โ€” either as a raw namespaced class (e.g., new Joomla\CMS\User\UserFactory),
    • via a placeholder like @jimport:JUser,
    • or from a Super Power entity used in a helper, controller, or model.
  3. Compile your component.
  4. Open the relevant compiled PHP file and check the use block at the top โ€” your reference should now appear.
  5. (Optional) Review debug logs to see what use statements were injected and from where.

๐Ÿง  Pro Tip: For custom logic blocks, wrap class usage in Insert Tags to preserve it across compiles โ€” the use statement will still be auto-managed.

Conclusion

Dynamic Use-Statement Generation brings modern PHP development best practices directly into your JCB workflow. You write logic โ€” JCB handles the imports. This means cleaner code, fewer runtime errors, better compatibility across Joomla versions, and no more hunting for missing namespaces.