Hello,
Thanks for taking the time to help me out with my problem. But my problem is i’m trying to autoload namespaced classes ex(“\FOO\BAR\FOOBAR”)
i would like to split out “FOOBAR” while still keeping the namespace intact. The problem is the namespace is wildcard in most cases.
Can you elaborate some more? For instance, where are you trying to obtain this information, from within the objects in question?
i was trying to accomplish this
if(\\preg_match('/\\\\\\\\/' , $className)){
$classParts = \\explode('\\\\' , $className);
$_count = \\count($classParts);
$_count--;
if(\\array_key_exists($_count , $classParts)){
$_class = $classParts[$_count];
unset($classParts[$_count]);
$_ns = \\implode('\\\\' , $classParts);
if(FALSE !== static::loadFromNamespace($_class , $_ns)){
return TRUE;
}else{
return FALSE;
}
}else{
return FALSE;
}
}
Like this? You’ll still need to do some sanity checking though…
<?php
define('SEPARATOR', '\\\\');
function getNamespace($path){
$parts = explode(SEPARATOR, $path);
array_pop($parts);
return implode(SEPARATOR, $parts);
}
function getClassname($path){
$parts = explode(SEPARATOR, $path);
return array_pop($parts);
}
$path = 'AnthonySterling\\Generic\\Object\\Thingy';
echo getNamespace($path), PHP_EOL;
echo getClassname($path), PHP_EOL;
/*
AnthonySterling\\Generic\\Object
Thingy
*/
This maybe of interest too.
well this for for autoloading witch i had to split out the namespace from classname to i can convert the namespace into a directory structure. This might help you understand what i was trying to do.
amespace Framework;
class Loader{
protected static $_autoloaders = array('\\Framework\\Loader' => 'autoload');
public static function loadFromPath($class , $path){
if(\\class_exists($class , FALSE) or \\interface_exists($class , FALSE)){
return TRUE;
}
if(\\file_exists(\\str_replace('\\\\' , '/' , $path) . '/' . $class . EXT)){
@require\\str_replace('\\\\' , '/' , $path) . '/' . $class . EXT;
if(\\class_exists($class , FALSE) or \\interface_exists($class , FALSE)){
return TRUE;
}
}
return FALSE;
}
public static function loadFromDir($class , $dir , $namespace = NULL){
if(\\class_exists($class , FALSE) or \\interface_exists($class , FALSE)){
return TRUE;
}
foreach(array(FW , APP) as $_path){
if(\\strtolower($dir) === 'framework' or \\strtolower($dir) === 'application' && \\file_exists($_path . $class . EXT)){
@require$_path . $class . EXT;
}elseif(\\file_exists($_path . $dir . DS . $class . EXT)){
@require $_path . $dir . DS . $class . EXT;
}
}
if(!\\is_null($namespace)){
$class = "\\\\$namespace\\\\$class";
}
if(\\class_exists($class , FALSE) or \\interface_exists($class , FALSE)){
return TRUE;
}
return FALSE;
}
public static function loadFromNamespace($class , $namespace = 'Framework'){
$_namespace = \\str_replace('Framework' , '' , $namespace);
$_path = \\str_replace('\\\\' , '/' , $_namespace);
$_path = \\ltrim($_path , '/');
$_path = \\rtrim($_path , '/');
$obj = static::loadFromDir($class , $_path , $namespace);
if(\\is_object($obj)){
return $obj;
}else{
return FALSE;
}
}
public static function autoload($className){
if(\\preg_match('/\\\\\\\\/' , $className)){
$classParts = \\explode('\\\\' , $className);
$_count = \\count($classParts);
$_count--;
if(\\array_key_exists($_count , $classParts)){
$_class = $classParts[$_count];
unset($classParts[$_count]);
$_ns = \\implode('\\\\' , $classParts);
if(FALSE !== static::loadFromNamespace($_class , $_ns)){
return TRUE;
}else{
return FALSE;
}
}else{
return FALSE;
}
}
$_autoloadPaths = static::_getAutoloadPaths();
if(isset($_autoloadPaths[$className])){
$_loaded = static::loadFromDir($className , $_autoloadPaths[$className]);
if($_loaded === TRUE){
return TRUE;
}
}
return FALSE;
}
public static function autoloadRegister(){
foreach(static::$_autoloaders as $_class => $_method){
if(\\class_exists($_class , FALSE) && \\method_exists($_class , $_method) && \\is_callable("$_class::$_method")){
return \\spl_autoload_register("$_class::$_method");
}else{
return FALSE;
}
}
return FALSE;
}
public function registerAutoloader($classname , $method){
static::$_autoloaders[$classname] = $method;
}
public function unregisterAutoloader($_method){
return \\spl_autoload_unregister($_method);
}
protected function _getAutoloadPaths(){
static::loadFromNamespace('Config' , 'Framework');
$_cfg = new \\Framework\\Config;
return $_cfg->getSetting('autoload');
}
}
?>
I understand what you’re trying to do, I’m just not sure if you have a question or not now?
I did at first but with some good testing i have created what i think is useful to my cuase. But on a side note i could be interested in hearing your critique of the code to maybe help me optimize and clean the code base.