• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

As designed Inconsistency: XF\Service\Thread\Creator::setUser() vs. XF\Service\Thread\Replies::setUser()

Affected version
2.0 DP 10

Kirby

Well-known member
#1
XF\Service\Thread\Creator::setUser() is protected, whereas XF\Service\Thread\Replier::setUser() is public.

This seems inconsistent to me and does make it rather cumbersome to post a new thread as a specific user:
PHP:
$creator = \XF::asVisitor($user, function() use ($forum)
{
	return \XF::service('XF:Thread\Creator', $forum);
});
instead of just
PHP:
$creator = \XF::service('XF:Thread\Creator', $forum);
$creator->setUser($user);
 

Chris D

XenForo developer
Staff member
#2
It's deliberate.

A bunch of other services (like the Replier) are now structured in the same was as the Creator service (which was finished after the release of DP10).

Your first example is exactly the approach that should be taken.

It's important to ensure that the user cannot change during the lifetime of the service, as that could result in some unexpected behaviour. With that in mind, the setUser method shouldn't be public, and the user should be set and stored based on the visitor User entity when the service is instantiated. If there's a need to run the service as a different user, that can be done using the asVisitor function.