SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot GOPalmer's Avatar
    Join Date
    Jan 2009
    Location
    Wiltshire, UK
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a loadObject method

    Hi,

    One of the sites I'm working on at the moment requires some sort of object loader. This is not something I've done before so I'm not really sure where to start. This is what I've come up with off the top of my head.

    PHP Code:
        public static function loadObject($className
        {
            
    $class 'Core_'.$className;
            
            
    $arguments func_get_args();
            unset(
    $arguments[0]);
            
            if (
    class_exists($class)) {
                if (
    is_callable(array($class,'getInstance'))) {
                   return 
    call_user_func_array(array($class'getInstance'), $arguments);
                }
                
    $arguments implode("', '"$arguments);
                eval(
    '$object = new '.$class."('$arguments');"); //must be a better way!!!!!
                
    return $object;
            }
            return 
    false;
        } 
    This approach is flawed in so so many ways. Is this something you've done before? Do you have any better ideas?

    Best Regards, George

  2. #2
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    Take a look to this code
    PHP Code:
    // (C) Andrea Giammarchi - Mit Style License
    function Factory($__CLASS__){
        static  
    $list = array();
        if(!isset(
    $list[$__CLASS__])){
            
    $list[$__CLASS__] = new ReflectionClass($__CLASS__);
        }
        
    $arguments func_get_args();
        
    array_shift($arguments);    
        return  
    $list[$__CLASS__]->getConstructor() ? $list[$__CLASS__]->newInstanceArgs($arguments) : $list[$__CLASS__]->newInstance();

    I hope this helps.

    Bye.

  3. #3
    SitePoint Zealot GOPalmer's Avatar
    Join Date
    Jan 2009
    Location
    Wiltshire, UK
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the code it works great. I've not used reflection before. Do you know of any good tutorials to get me started?

    Best Regards, George

  4. #4
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by GOPalmer View Post
    Thanks for the code it works great. I've not used reflection before. Do you know of any good tutorials to get me started?

    Best Regards, George
    Sorry, I don't know any good tutorials but
    you can take a look just at the reference

    http://uk.php.net/oop5.reflection


    Bye.


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
  •