JDocument/getInstance: Difference between revisions
From Joomla! Documentation
New page: ===Description===
Returns a reference to the global JDocument object, only creating it if it doesn't already exist.
<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki... |
m removing red link to edit, no existant pages |
||
| Line 3: | Line 3: | ||
<span class="editsection" style="font-size:76%;"> | <span class="editsection" style="font-size:76%;"> | ||
<nowiki>[< | <nowiki>[<! removed edit link to red link >]</nowiki> | ||
</span> | </span> | ||
<! removed transcluded page call, red link never existed > | |||
===Syntax=== | ===Syntax=== | ||
| Line 87: | Line 87: | ||
<span class="editsection" style="font-size:76%;"> | <span class="editsection" style="font-size:76%;"> | ||
<nowiki>[< | <nowiki>[<! removed edit link to red link >]</nowiki> | ||
</span> | </span> | ||
<! removed transcluded page call, red link never existed > | |||
===Examples=== | ===Examples=== | ||
| Line 102: | Line 102: | ||
format= ,,, | format= ,,, | ||
</dpl> | </dpl> | ||
[[Category:Archived pages API15]] | |||
Revision as of 14:03, 12 May 2013
Description
Returns a reference to the global JDocument object, only creating it if it doesn't already exist.
[<! removed edit link to red link >]
<! removed transcluded page call, red link never existed >
Syntax
& 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
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];
}
[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >
Examples
<CodeExamplesForm />