torf Posted March 24, 2023 Share Posted March 24, 2023 I was wondering: when I use the same hooks on different pages, is it better to only use that hook once and divide with if statements or is it ok to call a hook multiple times. Like: $wire->addHookBefore('Pages::saveReady', function(HookEvent $event){ $page = $event->arguments(0); if($page->hasField("field1")) { //do something } if($page->hasField("field2")) { //do something else } }); $wire->addHookAfter('Pages::unpublishReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->hasField("field1")) { //do something } if($page->hasField("field2")) { //do something else } }); or //Things regarding pages with Field 1 //###################### $wire->addHookBefore('Pages::saveReady', function(HookEvent $event){ $page = $event->arguments(0); if($page->hasField("field1")) { //do something } }); $wire->addHookAfter('Pages::unpublishReady', function(HookEvent $event) { if($page->hasField("field1")) { //do something else } }); //###################### //Things regarding pages with Field 2 //###################### $wire->addHookBefore('Pages::saveReady', function(HookEvent $event){ $page = $event->arguments(0); if($page->hasField("field2")) { //do something else } }); $wire->addHookAfter('Pages::unpublishReady', function(HookEvent $event) { if($page->hasField("field2")) { //do something else } }); //###################### Of course the first version looks nicer, but if you have a lot of functions it becomes increasingly confusing to keep track of those functions. The second version makes it much easier to keep logic together but I'm concerned if that will raise trouble with performance. 4 Link to comment Share on other sites More sharing options...
MarkE Posted March 24, 2023 Share Posted March 24, 2023 Interesting question. I am increasingly putting hooks in module ready() methods rather than ready.php, which naturally leans to the second approach, so I’d be interested if anyone has benchmarked this. Link to comment Share on other sites More sharing options...
Zeka Posted March 24, 2023 Share Posted March 24, 2023 I think that the first example would be more performant as every declaration of the hook creates an additional WireHook object that should be processed. While I think that difference in performance impact would be hard to measure in a typical PW site, I would stick with the first way not from the performance side, but from the side of structure, clarity, and repetition of the code, but it still depends on actual hook and what it is doing etc 1 Link to comment Share on other sites More sharing options...
torf Posted March 25, 2023 Author Share Posted March 25, 2023 @Zeka I wholeheartedly agree to most of what you wrote. The first version is more beautiful and presumably more performant. But as in most cases this is a small clients projects. Meaning nobody is ever going to look at that code and judge it by those standards. As soon as the page is online no one (including myself) will look at the code for a long period of time. Then my client will ask for some changes and I - or even worse - a new person will open that site and try to figure out how things are happening. And at this point the first version has its flaws. From the viewpoint of structure the second version (although not as beautiful) is better to handle. I have everything regarding one workflow at one point. So anyone not familiar with the code can look for "what happens with field1?" without gathering together bits and pieces shattered over half the document. Of course i can use comments but it's still like "what you are looking for is in shelf 1" vs. "What you are looking for is in shelf 1, 2, 5, 12 and some minor places I surely forgot to mention". So if performance issues are neglectable I'd rather prefer the second version for any page that is not maintained on a regular basis. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now