Userdata functions
Contents |
Introduction
The idea behind the userdata class is to make it easy for plugin developers to store and retrieve basic user-inputted information for use with their plugin --- account ids, display preferences, etc. --- without having to create a whole db table for it.
When to use it
If you're developing a plugin and find yourself about to create a whole db table just to store one row's worth of misc information related to your plugin, stop, follow the vague memory of this sentence back to this page, and check out this userdata method. Less lines of code, fewer keystrokes, no worrying about creating and installing the db in the install manifest.
How to use it
new
The basic idea here is to stick this line at the top of any script that uses the get() and set() functions. Replace "myplugin" with the name of your plugin folder. The other methods depend on this being set. If you don't do this first and try to call get() or set() php will bomb a fatal error.
$myplugin = new userdata("myplugin");
set()
Updates value of specific field (or inserts as new rec if fieldname not found)
$myplugin->set("firstname", "mike");
get()
- Syntax
- $myplugin->get([fieldname])
- If $fieldname is NOT passed - Gets all userdata related to passed plugin and returns it in an array
- If $fieldname IS passed - Gets data for requested field and returns it
Example #1: Get all userdata stored for your plugin in an associative array (indexed by fieldname). Think of this as doing a mysql_fetch_array() on that one-row table you were going to create before discovering this userdata stuff.
$myplugin->get();
Example #2: Get data from specific field.
$myplugin->get("firstname");
Example: Using them all
In this example, each available function is used.
$myplugin = new userdata("myplugin"); $myplugin->set("firstname", "John"); $myplugin->set("lastname", "Smith"); // This outputs "John Smith" echo $myplugin->get("firstname")." ".$myplugin->get("lastname"); // This also outputs "John Smith" $myData = get(); echo $myData['firstname']." ".$myData['lastname'];