JObject

From Joomla! Documentation

Jump to: navigation, search

JObject is a generic object class. It provides:

  • a mechanism that allows __construct() in PHP4;
  • operations for creating and accessing arbitrary object variables;
  • an array for storing errors (ordinary strings or JException objects) and operations for working with them.

Contents

Availability

Joomla 1.5 Joomla 1.6

Defined in

/joomla/base/object.php

Methods

Method name Description
__construct Class constructor, overridden in descendant classes.
JObject A hack to support __construct() on PHP 4
get Returns a property of the object or the default value if the property is not set
getError Get the most recent error message
getErrors Return all errors, if any
getProperties Returns an associative array of object properties
getPublicProperties Legacy Method, use JObject::getProperties() instead
set Modifies a property of the object, creating it if it does not already exist
setError Add an error message
setProperties Set the object properties based on a named array/hash
toString Object-to-string conversion

Importing

jimport( 'joomla.base.object' );

Example

$bunny = new JObject;
$bunny->set('animalType', 'hare');
$bunny->set('color', 'white');
$bunny->set('ears', 2);
 
$wolfy = new JObject;
$wolfy->setProperties(
                     array(
                           'animalType'=>'wolf',
                           'color'=>'grey',
                           'teeth'=>42,
                           'stomach'=>null
                           )
                     );
 
 
$wolfy->stomach = $bunny;
 
print_r($wolfy);

would output

JObject Object
(
    [_errors] => Array
        (
        )

    [animalType] => wolf
    [color] => grey
    [teeth] => 42
    [stomach] => JObject Object
        (
            [_errors] => Array
                (
                )

            [animalType] => hare
            [color] => white
            [ears] => 2
        )

)

See also

Personal tools