Jump to content

Use cases for aliases


Zeka
 Share

Recommended Posts

Hey @Zeka!

I assume you're referring to "method aliases" from MethodPropsTrait?

If so, this is a feature that, to be honest, I've not had much use for myself. As far as I can remember, the original intention was two-fold:

1) Whether to make code cleaner or to mitigate issues resulting from partial rewrites, one can create an alias or "virtual method" that calls another local method behind the scenes. Say, if a controller class had method called "words", but later it was decided that it should be "terms" instead (or for some reason front-end code requires it to be that) yet changing it now could potentially cause other issues, one could handle this via an alias:

class DictonaryController extends \Wireframe\Controller {

	protected $method_aliases = [
		'terms' => [
			'callable' => ['self', 'words'],
			'params' => [],
		],
	];

	public function words(): array {
		return $this->pages->findRaw('parent=' . $this->page->id . ', template=Word', [
			'name',
			'title',
			'dictionary_category.title',
		], [
			'objects' => true,
			'entities' => true,
		]);
	}

}

Here I'm defining the $method_aliases property directly on class declaration, but in some cases it could make more sense to define individual aliases via the API: $this->setAlias($alias, $callable, $params).

2) Another use case is referring to a method defined somewhere else — another controller, utility class, third party library, etc. Essentially this makes it easy to inject new methods or properties into classes implementing the MethodPropsTrait (such as controllers and components):

class DictonaryController extends \Wireframe\Controller {

	public function init() {
		$this->setAlias('terms', '\Wireframe\Lib\Utils::getWords', ['page' => $this->page]);
	}

}

 You can do the same with ProcessWire hooks or just have a method that calls another method, so this is largely a matter of preference ?

---

As I mentioned above, I've not had much use for this feature myself. It's been there since the very beginning, and it's a concept that seemed potentially useful (not to mention fun) while I was working on the initial implementation of Wireframe, but real world use cases have been few and far between.

If you can think of other use cases, let me know ?

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

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...