I don't know how this code can work

SneakyDave

Well-known member
This is probably a dumb OOP question, and hopefully it's something easy that I just can't see.
EDIT: No, I just misinterpreted the first line.

I haven't had to do much development with models until recently, so again, excuse the ignorance.

Anyway, this is a piece of code that I was looking at, the easiest way to implement a model into an addon, This bit of code is from WidgetFramework, but it appears to be pretty consistent across other addons. This is from the WidgetRenderer that shows the latest ProfilePosts.
Code:
$core = WidgetFramework_Core::getInstance();
$visitor = XenForo_Visitor::getInstance();
$profilePostModel = $core->getModelFromCache('XenForo_Model_ProfilePost');

$profilePosts = $profilePostModel->WidgetFramework_getLatestProfilePosts(array(
   'deleted' => false,
   'moderated' => false
), array(
   'limit' => $widget['options']['limit'] * 3,
   'join' => XenForo_Model_ProfilePost::FETCH_USER_POSTER | XenForo_Model_ProfilePost::FETCH_USER_RECEIVER | XenForo_Model_ProfilePost::FETCH_USER_RECEIVER_PRIVACY,
   'permissionCombinationId' => $visitor->permission_combination_id
));

The first 3 lines are no biggie, they are simple enough to understand.

The fifth line is what goes and executes the SQL from _getLatestProfilePosts method. But the method name appears to me like it shouldn't exist, WidgetFramework_getLatestProfilePosts()? Shouldn't it be the private _getLatestProfilePosts() method from the XenForo_Model_ProfilePost class?

When the $profilePostModel object is instantiated, how can WidgetFramework methods be available if it's a native XenForo ProfilePost model?

Am I missing something? Isn't the $profilePostModel->WidgetFramework_getLatestProfilePosts() statement calling a method from the $profilePostModel object?

OK, the other question, and maybe I don't even want to ask it, is that I've seen a lot of constants, like the ones used on line 5, like the FETCH_USER_ constants.
Code:
const FETCH_USER_POSTER = 0x01;

What are these hex values used for, why are they hex? Are they just binary representations of a column on a table?

Ok, that's it, thanks in advance for any help. I'll keep digging and seeing if I can interpret what's going on.
 
Last edited:
ok, as an update, maybe I'm just confused, I see that the
WidgetFramework_getLatestProfilePosts() method is actually part of the
WidgetFramework_XenForo_Model_ProfilePost class, not the XenForo_Model_ProfilePost class, and it extends the
"special" XFCP_WidgetFramework_XenForo_Model_ProfilePost class.

So unless I'm mistaken, the reason that this works is the automatic creating of the XFCP special class.
 
OK, one last comment, the first line doesn't refer to XenForo core, like I thought, it refers to the WidgetFramework core, so everything is based off of that. So...

Code:
$core->getModelFromCache('XenForo_Model_ProfilePost');

Is really getting the library/WidgetFramework/XenForo/Model/ProfilePost class, not the native Xenforo ProfilePost class.

I think that explains it now.
 
Top Bottom