XoopsObject Class
The XoopsObject class is the fundamental base class for all data objects in the XOOPS system. It provides a standardized interface for managing object properties, validation, dirty tracking, and serialization.
Class Overview
Section titled “Class Overview”namespace Xoops\Core;
class XoopsObject{ protected array $vars = []; protected array $cleanVars = []; protected bool $isNew = true; protected array $errors = [];}Class Hierarchy
Section titled “Class Hierarchy”XoopsObject├── XoopsUser├── XoopsGroup├── XoopsModule├── XoopsBlock├── XoopsComment├── XoopsNotification├── XoopsConfig└── [Custom Module Objects]Properties
Section titled “Properties”| Property | Type | Visibility | Description |
|---|---|---|---|
$vars | array | protected | Stores variable definitions and values |
$cleanVars | array | protected | Stores sanitized values for database operations |
$isNew | bool | protected | Indicates if object is new (not yet in database) |
$errors | array | protected | Stores validation and error messages |
Constructor
Section titled “Constructor”public function __construct()Creates a new XoopsObject instance. The object is marked as new by default.
Example:
$object = new XoopsObject();// Object is new and has no defined variablesCore Methods
Section titled “Core Methods”initVar
Section titled “initVar”Initializes a variable definition for the object.
public function initVar( string $key, int $dataType, mixed $value = null, bool $required = false, int $maxlength = null, string $options = ''): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | Variable name |
$dataType | int | Data type constant (see Data Types) |
$value | mixed | Default value |
$required | bool | Whether field is required |
$maxlength | int | Maximum length for string types |
$options | string | Additional options |
Data Types:
| Constant | Value | Description |
|---|---|---|
XOBJ_DTYPE_TXTBOX | 1 | Text box input |
XOBJ_DTYPE_TXTAREA | 2 | Textarea content |
XOBJ_DTYPE_INT | 3 | Integer value |
XOBJ_DTYPE_URL | 4 | URL string |
XOBJ_DTYPE_EMAIL | 5 | Email address |
XOBJ_DTYPE_ARRAY | 6 | Serialized array |
XOBJ_DTYPE_OTHER | 7 | Custom type |
XOBJ_DTYPE_SOURCE | 8 | Source code |
XOBJ_DTYPE_STIME | 9 | Short time format |
XOBJ_DTYPE_MTIME | 10 | Medium time format |
XOBJ_DTYPE_LTIME | 11 | Long time format |
XOBJ_DTYPE_FLOAT | 12 | Floating point |
XOBJ_DTYPE_DECIMAL | 13 | Decimal number |
XOBJ_DTYPE_ENUM | 14 | Enumeration |
Example:
class MyObject extends XoopsObject{ public function __construct() { parent::__construct(); $this->initVar('id', XOBJ_DTYPE_INT, null, false); $this->initVar('title', XOBJ_DTYPE_TXTBOX, '', true, 255); $this->initVar('content', XOBJ_DTYPE_TXTAREA, '', false); $this->initVar('email', XOBJ_DTYPE_EMAIL, '', true, 100); $this->initVar('created', XOBJ_DTYPE_INT, time(), false); $this->initVar('status', XOBJ_DTYPE_INT, 1, true); }}setVar
Section titled “setVar”Sets the value of a variable.
public function setVar( string $key, mixed $value, bool $notGpc = false): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | Variable name |
$value | mixed | Value to set |
$notGpc | bool | If true, value is not from GET/POST/COOKIE |
Returns: bool - True if successful, false otherwise
Example:
$object = new MyObject();$object->setVar('title', 'Hello World');$object->setVar('content', '<p>Content here</p>', true); // Not from user input$object->setVar('status', 1);getVar
Section titled “getVar”Retrieves the value of a variable with optional formatting.
public function getVar( string $key, string $format = 's'): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | Variable name |
$format | string | Output format |
Format Options:
| Format | Description |
|---|---|
's' | Show - HTML entities escaped for display |
'e' | Edit - For form input values |
'p' | Preview - Similar to show |
'f' | Form data - Raw for form processing |
'n' | None - Raw value, no formatting |
Returns: mixed - The formatted value
Example:
$object = new MyObject();$object->setVar('title', 'Hello <World>');
echo $object->getVar('title', 's'); // "Hello <World>"echo $object->getVar('title', 'e'); // "Hello <World>" (for input value)echo $object->getVar('title', 'n'); // "Hello <World>" (raw)
// For array data types$object->setVar('options', ['a', 'b', 'c']);$options = $object->getVar('options', 'n'); // Returns arraysetVars
Section titled “setVars”Sets multiple variables at once from an array.
public function setVars( array $values, bool $notGpc = false): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$values | array | Associative array of key => value pairs |
$notGpc | bool | If true, values are not from GET/POST/COOKIE |
Example:
$object = new MyObject();$object->setVars([ 'title' => 'My Title', 'content' => 'My content', 'status' => 1]);
// From database (not user input)$object->setVars($row, true);getValues
Section titled “getValues”Retrieves all variable values.
public function getValues( array $keys = null, string $format = 's', int $maxDepth = 1): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$keys | array | Specific keys to retrieve (null for all) |
$format | string | Output format |
$maxDepth | int | Maximum depth for nested objects |
Returns: array - Associative array of values
Example:
$object = new MyObject();
// Get all values$allValues = $object->getValues();
// Get specific values$subset = $object->getValues(['title', 'status']);
// Get raw values for database$rawValues = $object->getValues(null, 'n');assignVar
Section titled “assignVar”Assigns a value directly without validation (use with caution).
public function assignVar( string $key, mixed $value): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | Variable name |
$value | mixed | Value to assign |
Example:
// Direct assignment from trusted source (e.g., database)$object->assignVar('id', $row['id']);$object->assignVar('created', $row['created']);cleanVars
Section titled “cleanVars”Sanitizes all variables for database operations.
public function cleanVars(): boolReturns: bool - True if all variables are valid
Example:
$object = new MyObject();$object->setVar('title', 'Test');$object->setVar('email', 'user@example.com');
if ($object->cleanVars()) { // Variables are sanitized and ready for database $cleanData = $object->cleanVars;} else { // Validation errors occurred $errors = $object->getErrors();}Checks or sets whether the object is new.
public function isNew(): boolpublic function setNew(): voidpublic function unsetNew(): voidExample:
$object = new MyObject();echo $object->isNew(); // true
$object->unsetNew();echo $object->isNew(); // false
$object->setNew();echo $object->isNew(); // trueError Handling Methods
Section titled “Error Handling Methods”setErrors
Section titled “setErrors”Adds an error message.
public function setErrors(string|array $error): voidExample:
$object->setErrors('Title is required');$object->setErrors(['Field 1 error', 'Field 2 error']);getErrors
Section titled “getErrors”Retrieves all error messages.
public function getErrors(): arrayExample:
$errors = $object->getErrors();foreach ($errors as $error) { echo $error . "\n";}getHtmlErrors
Section titled “getHtmlErrors”Returns errors formatted as HTML.
public function getHtmlErrors(): stringExample:
if (!$object->cleanVars()) { echo '<div class="error">' . $object->getHtmlErrors() . '</div>';}Utility Methods
Section titled “Utility Methods”toArray
Section titled “toArray”Converts the object to an array.
public function toArray(): arrayExample:
$object = new MyObject();$object->setVar('title', 'Test');$data = $object->toArray();// ['title' => 'Test', ...]getVars
Section titled “getVars”Returns the variable definitions.
public function getVars(): arrayExample:
$vars = $object->getVars();foreach ($vars as $key => $definition) { echo "Field: $key, Type: {$definition['data_type']}\n";}Complete Usage Example
Section titled “Complete Usage Example”<?php/** * Custom Article Object */class Article extends XoopsObject{ /** * Constructor - Initialize all variables */ public function __construct() { parent::__construct();
// Primary key $this->initVar('article_id', XOBJ_DTYPE_INT, null, false);
// Required fields $this->initVar('title', XOBJ_DTYPE_TXTBOX, '', true, 255); $this->initVar('author_id', XOBJ_DTYPE_INT, 0, true);
// Optional fields $this->initVar('summary', XOBJ_DTYPE_TXTAREA, '', false); $this->initVar('content', XOBJ_DTYPE_TXTAREA, '', false); $this->initVar('category_id', XOBJ_DTYPE_INT, 0, false);
// Timestamps $this->initVar('created', XOBJ_DTYPE_INT, time(), false); $this->initVar('updated', XOBJ_DTYPE_INT, time(), false);
// Status flags $this->initVar('published', XOBJ_DTYPE_INT, 0, false); $this->initVar('views', XOBJ_DTYPE_INT, 0, false);
// Metadata as array $this->initVar('meta', XOBJ_DTYPE_ARRAY, [], false); }
/** * Get formatted creation date */ public function getCreatedDate(string $format = 'Y-m-d H:i:s'): string { return date($format, $this->getVar('created', 'n')); }
/** * Check if article is published */ public function isPublished(): bool { return $this->getVar('published', 'n') == 1; }
/** * Increment view counter */ public function incrementViews(): void { $views = $this->getVar('views', 'n'); $this->setVar('views', $views + 1); }
/** * Custom validation */ public function validate(): bool { $this->errors = [];
// Title validation $title = trim($this->getVar('title', 'n')); if (empty($title)) { $this->setErrors('Title is required'); } elseif (strlen($title) < 5) { $this->setErrors('Title must be at least 5 characters'); }
// Author validation if ($this->getVar('author_id', 'n') <= 0) { $this->setErrors('Author is required'); }
return empty($this->errors); }}
// Usage$article = new Article();$article->setVar('title', 'My First Article');$article->setVar('author_id', 1);$article->setVar('content', '<p>Article content here...</p>', true);$article->setVar('meta', [ 'keywords' => ['xoops', 'cms', 'php'], 'description' => 'An example article']);
if ($article->validate() && $article->cleanVars()) { // Save to database via handler $handler = xoops_getModuleHandler('article', 'mymodule'); $handler->insert($article);
echo "Article saved with ID: " . $article->getVar('article_id');} else { echo "Errors: " . $article->getHtmlErrors();}Best Practices
Section titled “Best Practices”-
Always Initialize Variables: Define all variables in the constructor using
initVar() -
Use Appropriate Data Types: Choose the correct
XOBJ_DTYPE_*constant for validation -
Handle User Input Carefully: Use
setVar()with$notGpc = falsefor user input -
Validate Before Saving: Always call
cleanVars()before database operations -
Use Format Parameters: Use the appropriate format in
getVar()for the context -
Extend for Custom Logic: Add domain-specific methods in subclasses
Related Documentation
Section titled “Related Documentation”- XoopsObjectHandler - Handler pattern for object persistence
- ../Database/Criteria - Query building with Criteria
- ../Database/XoopsDatabase - Database operations
See also: XOOPS Source Code