Actions

API16

JDocument/getInstance

From Joomla! Documentation

< API16:JDocument
Revision as of 17:44, 22 March 2010 by Doxiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Returns the global JDocument object, only creating it if it doesn't already exist.

[Edit Descripton]

Description:JDocument/getInstance

Syntax

static getInstance($type= 'html', $attributes=array())
Parameter Name Default Value Description
$type 'html' $type The document type to instantiate
$attributes array()

Returns

object The document object.

Defined in

libraries/joomla/document/document.php

Importing

jimport( 'joomla.document.document' );

Source Body

public static function getInstance($type = 'html', $attributes = array())
{
        static $instances;
 
        if (!isset($instances)) {
                $instances = array();
        }
 
        $signature = serialize(array($type, $attributes));
 
        if (empty($instances[$signature]))
        {
                $type   = preg_replace('/[^A-Z0-9_\.-]/i', '', $type);
                $path   = dirname(__FILE__).DS.$type.DS.$type.'.php';
                $ntype  = null;
 
                // Check if the document type exists
                if (!file_exists($path))
                {
                        // Default to the raw format
                        $ntype  = $type;
                        $type   = 'raw';
                }
 
                // Determine the path and class
                $class = 'JDocument'.$type;
                if (!class_exists($class))
                {
                        $path   = dirname(__FILE__).DS.$type.DS.$type.'.php';
                        if (file_exists($path)) {
                                require_once $path;
                        } else {
                                JError::raiseError(500,JText::_('Unable to load document class'));
                        }
                }
 
                $instance       = new $class($attributes);
                $instances[$signature] = &$instance;
 
                if (!is_null($ntype))
                {
                        // Set the type to the Document type originally requested
                        $instance->setType($ntype);
                }
        }
 
        return $instances[$signature];
}

[Edit See Also] SeeAlso:JDocument/getInstance

Examples

<CodeExamplesForm />