Skip to content

Instantly share code, notes, and snippets.

@AmyStephen
Last active December 11, 2015 00:59
Show Gist options
  • Save AmyStephen/4520559 to your computer and use it in GitHub Desktop.
Save AmyStephen/4520559 to your computer and use it in GitHub Desktop.
JPlatform Namespace Examples
<?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;
}
}
<?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;
}
}
<?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.');
}
}
}
<?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;
}
}
@AmyStephen
Copy link
Author

Above should define full usage of namespaces.

What that means is that the following approaches would not be permitted:

  1. Unless there is a compelling reason to do so, namespaces should not be defined in a variable.
  2. Namespaces should not be contained within the code. Only identified at the top.
  3. Do not import folders, all classes should be specifically defined.
  4. Do not import classes within the current namespace.

Doing those things means - the top of the page information specifically defines all requirements. The code is not obstructed with namespace code.

@dongilbert
Copy link

These all look really good. Thanks for those examples Amy.

@Xerosigma
Copy link

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