Skip to content

Instantly share code, notes, and snippets.

@RalfAlbert
Created December 18, 2011 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RalfAlbert/1492559 to your computer and use it in GitHub Desktop.
Save RalfAlbert/1492559 to your computer and use it in GitHub Desktop.
Selector with Namespaces
<?php
/* Plugin Name Some Code */
namespace PluginInit
{
/**
* mocking code
*/
CONST WP_VERSION = '3.3';
function get_wp_version(){ return '3.3' ; }
function get_wp_network() { return FALSE; }
/*// end mocking code */
function init(){
// check versions
$_wp_version = (int) version_compare( WP_VERSION, get_wp_version(), '>=' );
$_network = (int) get_wp_network();
$resources = new \stdClass();
$resources->wp = array( 'wp30', 'wp33' );
$resources->network = array( 'nonetwork', 'network' );
$mynamespace = 'lib\\' . $resources->wp[$_wp_version] . '\\'
. $resources->network[$_network];
define( 'NSPACE', $mynamespace );
}
init();
}
namespace PluginCode
{
$filename = sprintf('%1$s%2$s%1$sthe_code.php',DIRECTORY_SEPARATOR, str_replace( '\\', DIRECTORY_SEPARATOR, NSPACE ) );
require_once $filename;
$class = NSPACE . '\Test';
$c = new $class();
$c::test2();
$func = NSPACE . '\test';
$func();
}
<?php
namespace lib\wp33\nonetwork
{
class Test{
public function __construct(){
echo 'WordPress Version größer-gleich 3.3, kein Netzwerk<br />';
}
public static function test2(){
printf( "Dies ist die Methode <strong>%s</strong><br />", __METHOD__ );
}
}
function test(){
printf( "Wir sind im Namensraum <strong>%s</strong><br />", __NAMESPACE__ );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment