SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    split classname and namepspace

    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.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Can you elaborate some more? For instance, where are you trying to obtain this information, from within the objects in question?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i was trying to accomplish this

    PHP Code:
    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;
                }
            } 

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Like this? You'll still need to do some sanity checking though...

    Code PHP:
    <?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.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    PHP Code:
    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)){
                [
    MENTION=36061]require[/MENTION]\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)){
                    [
    MENTION=36061]require[/MENTION]$_path $class EXT;
                }elseif(\
    file_exists($_path $dir DS $class EXT)){
                    [
    MENTION=36061]require[/MENTION$_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');
        }
    }

    ?> 
    Last edited by Stevie D; Dec 21, 2011 at 16:26. Reason: Tried to correct code where @ tagging had messed it up, but failed :-(

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I understand what you're trying to do, I'm just not sure if you have a question or not now?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •