Last active
December 11, 2015 00:59
-
-
Save AmyStephen/4520559 to your computer and use it in GitHub Desktop.
JPlatform Namespace Examples
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
<?php | |
/** | |
* @package Joomla.Platform | |
* @subpackage Document | |
* | |
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE | |
*/ | |
namespace Joomla\Example1; | |
defined('JPATH_PLATFORM') or die; | |
class Classname1 | |
{ | |
protected $classname2; | |
public function __construct() | |
{ | |
// Do not import classes within the same namespace | |
$this->classname2 = new Classname2; | |
} | |
} |
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
<?php | |
/** | |
* @package Joomla.Platform | |
* @subpackage Document | |
* | |
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE | |
*/ | |
namespace Joomla\Example2; | |
defined('JPATH_PLATFORM') or die; | |
// Import classes external to the namespace | |
// Never import folders, instead specifically import each class | |
use Joomla\External\Nameoftheclass; | |
// only define an aliase when there is a conflict | |
// The alias must be the node previous the class name + the class name | |
use Joomla\External\Class\Helper as ClassHelper; | |
use Joomla\Another\Helper as AnotherHelper; | |
class Classname1 | |
{ | |
protected $classname2; | |
protected $classhelper; | |
protected $anotherhelper; | |
public function __construct() | |
{ | |
// Only use the class name within the code | |
$this->classname2 = new Nameoftheclass; | |
// Only use the alias name within the code when the alias is required | |
$this->classhelper = new ClassHelper; | |
$this->anotherhelper = new AnotherHelper; | |
} | |
} |
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
<?php | |
/** | |
* @package Joomla.Platform | |
* @subpackage Document | |
* | |
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE | |
*/ | |
namespace Joomla\Example3; | |
defined('JPATH_PLATFORM') or die; | |
// Import PHP standard functions with the use statement | |
use Exception; | |
use RuntimeException; | |
class Classname1 | |
{ | |
public function __construct() | |
{ | |
Try | |
{ | |
$results = new Classname2; | |
} | |
// use the imported global name within the code | |
catch (Exception $e) | |
{ | |
throw new RuntimeException ('This thing did not work.'); | |
} | |
} | |
} |
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
<?php | |
/** | |
* @package Joomla.Platform | |
* @subpackage Document | |
* | |
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE | |
*/ | |
namespace Joomla\Example4; | |
defined('JPATH_PLATFORM') or die; | |
// alias due to the conflict with the classname in the namespace | |
use Joomla\External\Classname2 as ExternalClassname2; | |
class Classname1 | |
{ | |
/** | |
* If there is an the alias use it in the phpDoc, not the path | |
* | |
* @var ExternalClass $input | |
* | |
* @return ExternalClass | |
* @since 1.0 | |
*/ | |
public function method1(ExternalClassname2 $input) | |
{ | |
return $input; | |
} | |
/** | |
* Use the class name for classes within the same namespace in phpDoc, not the path | |
* | |
* @var Classname2 $input | |
* | |
* @return Classname2 | |
* @since 1.0 | |
*/ | |
public function method2(Classname2 $input) | |
{ | |
return $input; | |
} | |
} |
These all look really good. Thanks for those examples Amy.
This is fantastic, just what I was looking for. Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Above should define full usage of namespaces.
What that means is that the following approaches would not be permitted:
Doing those things means - the top of the page information specifically defines all requirements. The code is not obstructed with namespace code.