MyBB Community Forums

Full Version: Some Modding Tips [For Newbies]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here is a tip about globals in functions

If you like to code your mod using functions but you get an error saying something like "Call to a member function on a non-object" then that's where globals come in.

The globals I use are
global $db, $lang, $mybb, $templates, $header, $headerinclude, $theme, $footer;

From my experience..
If it has an arrow after it then it should be called in the globals (ie. $db->query) or if it's used in the template file ($header, $headerinclude, $theme, $footer)

Here's a sample code using a function an globals
function test()
{
 global $db, $lang, $mybb, $templates, $header, $headerinclude, $theme, $footer;
   
 $mybb->usergroup['cancp'] == "yes")
   {
   $sometext = $lang->sometext;
  }
    $query = $db->query("SELECT * FROM ".TABLE_PREFIX."test WHERE testid='".intval($mybb->input['testid'])."'");
    $result = $db->fetch_array($query);

	eval("\$test = \"".$templates->get("test")."\";");
	outputpage($test);
}


I can't exactly explain why it works like that but hopefully someone will shine some light on it.
Globals
You use global when you want to use an already defined variable in a function without passing it as an argument, as it can be changed if it is an argument. And besides that, if you sent $db or $lang through the arguments, it would loose it's ->blah value.
Also, you should only call the globals that you are using in the function, or else it takes up time and memory.

$lang
You can only use $lang->value if value is assigned. You have to go into the lang files and assign it (don't ask me how; I'm just running of what I know from PHP experience and other software experience). If $lang->sometext isn't assigned in the language files, then it won't work when you try to call it.

As for eval, I believe it takes a string and parses it as PHP. I don't see why it needs to be used here, but I guess it doesn't hurt anything...

EDIT: Thanks for doing this thread... it gave me a view of how the mybb system thing works so that I don't have to download all the files and snoop through them for even an hour! Toungue I have just come from phpBB, and since this is somewhat similar, it isn't too hard to learn. However, it is very different in some areas, so this helped me a little.
Function Names
Make sure your function names are unique and try not to use functions names that others might use. Try appending a few letters to your function name that are to do with your mod.
The only time you have to append your functions with the filename of the plugin, is for activate and deactivate functions. For example, the mod is called helloworld.php (like the example plugin provided with MyBB). When the plugin is activated it will call helloworld_activate. The functions that this plugin uses can be called anything. But it's helpful for other modders if you do the same thing to all your functions.

The rebuild settings function (rebuilds your setting cache) is called after your mod has been activated/deactivated. You no longer have to tell users to change settings once the mod is activated (unlike RC4).

Setting Groups
If you are inserting a settings group, then you can use the code below
$db->query("INSERT INTO " . TABLE_PREFIX . "settinggroups(name, description, disporder, isdefault) VALUES('Setting Group name', 'Description', 7, 'yes')");
$gid = $db->insert_id();
$order = 0;
$db->query("INSERT INTO " . TABLE_PREFIX . "settings(name, title, description, optionscode, value, disporder, gid) VALUES('test_setting', 'Setting Name', 'Description', 'text', '', ".++$order.", $gid)");
That will insert the group, get it's id which you can then use for the actual settings. Each setting SQL statement also has an incremented order value, so if you move the SQL statements around in PHP, that will make the settings appear in the same order in the Admin CP. This saves you having to manually change the order values.