Created
December 18, 2011 06:29
-
-
Save RalfAlbert/1492559 to your computer and use it in GitHub Desktop.
Selector with Namespaces
This file contains 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 | |
/* 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(); | |
} | |
This file contains 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 | |
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