Created
January 4, 2013 22:56
-
-
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also, should get rid of those static instances - ignore them in the examples.