Skip to content

Instantly share code, notes, and snippets.

@AmyStephen
Created January 4, 2013 22:56
Show Gist options
  • Save AmyStephen/4458255 to your computer and use it in GitHub Desktop.
Save AmyStephen/4458255 to your computer and use it in GitHub Desktop.
Implications of use statements on overrides - JDocument Relates to: https://github.com/joomla/joomla-platform/blob/staging/libraries/joomla/document/document.php Example 1: uses dependency injection to pass in the Namespace and the Alias. This enables overriding the location of the class file so that the class can be overridden. The core HTML cl…
namespace Joomla\Document;
class JDocument
{
public function __construct($options = array())
{
if (array_key_exists('lineend', $options))
{
$this->setLineEnd($options['lineend']);
}
// etc
if (array_key_exists('namespace', $options))
{
$this->setNamespace($options['namespace']);
}
}
public static function getInstance($type = 'html', $attributes = array())
{
// instead of hardcoding the path - as it is now
$alias = $this->getNamespaceAlias; // ex. Html
$ns = $this->getNamespace; // ex. Joomla\Document\Html
$class = $ns . '\' . $alias
return new $class();
}
//etc
}
namespace Joomla\Document;
use Joomla\Document\Html\Html;
class JDocument
{
public function __construct($options = array())
{
if (array_key_exists('lineend', $options))
{
$this->setLineEnd($options['lineend']);
}
// etc
// do not use DI to pass in NS.
}
public static function getInstance($type = 'html', $attributes = array())
{
// this uses the hardcoded use statement to locate the file
$class = $alias;
return new $class();
}
//etc
}
@AmyStephen
Copy link
Author

Also, should get rid of those static instances - ignore them in the examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment