Jump to content

Correct use of Wire::_callMethod()


Robin S
 Share

Recommended Posts

I'm wondering about the intended or correct use of Wire::_callMethod().

I was tying out a hook to ProcessPageView::execute(), and I wanted to get the Page object that execute() was being called for. ProcessPageView has a getPage() method for that purpose, but it's a protected method so this doesn't work:

$wire->addHookBefore('ProcessPageView::execute', function(HookEvent $event) {
    /* @var ProcessPageView $ppv */
    $ppv = $event->object;
    $page = $ppv->getPage();
    // ...
});

But then I found Wire::_callMethod(), and this does work:

$wire->addHookBefore('ProcessPageView::execute', function(HookEvent $event) {
    /* @var ProcessPageView $ppv */
    $ppv = $event->object;
    $page = $ppv->_callMethod('getPage', []);
    // ...
});

Is this a correct use of _callMethod()? Is this what _callMethod() exists for? The code comment for _callMethod() is...

/**
 * Call a method in this object, for use by WireHooks
 * 
 * #pw-internal
 *

...so the "for use by WireHooks" part sounds like my usage is okay, but the "#pw-internal" and the underscore prefix made me wonder if this method is only intended for use by the core. I couldn't find any mentions in the forum of anyone using it in their own code.

  • Like 1
Link to comment
Share on other sites

Disregarding the viability of using _callMethod() here: While I understand that ProcessPageView is awefully structured for hooks – I tried to make it work with a testing framework last year – I'd not suggest you calling getPage from your hook if you don't intend to replace the whole execute function. getPage() is not safe to be called multiple times, because it does modify global state and therefore does not return the same value on multiple invocations. Also I doubt searching for the called page multiple times would be good for your page response times anyways.

  • Like 2
Link to comment
Share on other sites

Thanks for the info.

11 hours ago, LostKobrakai said:

Also I doubt searching for the called page multiple times would be good for your page response times anyways.

Hooking ProcessPageView::execute and calling getPage() seems to have negligible effect on response times. The method only executes once per page view, and getting the page object with getPage() method took an average of 0.0015 seconds in my tests. So I'm not too worried about that aspect.

 

12 hours ago, LostKobrakai said:

getPage() is not safe to be called multiple times, because it does modify global state and therefore does not return the same value on multiple invocations

I'm not sure what you mean here - it might return the page object for a different page? Or some aspect of the page can be different? This gave me the same ID each time:

$wire->addHookBefore('ProcessPageView::execute', function(HookEvent $event) {
    /* @var ProcessPageView $ppv */
    $ppv = $event->object;
    for($i = 0; $i < 50; $i++) {
        $page = $ppv->_callMethod('getPage', []);
        bd($page->id, 'page->id');
    }
});

 

  • Like 2
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...