Jump to content

Is it possible to use utility class in component view if it is explicitly used only at component controller level?


Clarity
 Share

Recommended Posts

Hello everyone!

I'm using "use" statement in controller like this:

<?php

namespace Wireframe\Component\MyComponent;

use Wireframe\Lib\Utils;

class MyComponent extends \Wireframe\Component {
  ...
}
  

Then it works in component view iff I write

use Wireframe\Lib\Utils;

in component view, otherwise I get an error: Class "ProcessWire\Utils" not found.

Can you please tell me if it is possible to use utility class in component view if it is explicitly used only in component controller?

Link to comment
Share on other sites

I don't think there's any obvious way around this: PHP considers namespace declarations and use statements (aliasing/importing) on a per file basis.

Quote

Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.

(Technically this could be achieved by some sort of file complication scheme, but in my opinion issues with that approach would easily outweight potential benefits.)

It's a matter of preference of course, but I often use full namespace (e.g. \Wireframe\Lib\Utils::some_method()) in my views. Just feels more intuitive to me, even if it does add a bit of verbosity 🙂

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...