SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2003
    Location
    Melbourne, Victoria, Australia
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Accessing static methods of classes...

    Hi all,

    Why does this work:
    PHP Code:
    $model =& Menu::instance() 
    and this doesn't?
    PHP Code:
    $className "Menu"
    $model =& $className::instance() 
    I can always create it using new which works fine, but I am calling the method instance() to return the existing object if it has already been created, otherwise, create a new one (Singleton class I think right?)

    PHP Code:
    // part of the Menu class 
    function &instance() { 
        static 
    $model
        if (!isset(
    $model)) { 
            
    $model = new Menu(); 
            return 
    $model
        } 


    Thanks,
    Carl

  2. #2
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Hmmmm, it should work as far as I know Maybe give this a shot
    PHP Code:
    $className "Menu";
    $model =& {$className}::instance(); 
    Edit:


    Just tried it here and I couldn't get either way to work I am using PHP5 which may have a difference but I doubt it.

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2003
    Location
    Melbourne, Victoria, Australia
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, didn't work for me either (still on PHP4.x).

    What do the {} braces imply around a variable?

  4. #4
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Variable variables, I just thought it might help here. Figured it was worth a shot

    http://au.php.net/manual/en/function...able-functions
    http://au.php.net/manual/en/language.variables.variable

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2003
    Location
    Melbourne, Victoria, Australia
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Perhaps there's another way around this one (note, I'm quite new to OO programming so excuse to imcorrect terminology!) I've posted the important bits of the code below to give you an idea of what I'm trying to accomplish here.

    Basically I want a dynamic way of instantiating and returning a class object (and return the existing one if it's already been created).

    PHP Code:
    // Controller
     
    $mf = new ModelFactory();
     
    $menuModel =& $mf->Build'menu' ); 
    PHP Code:
    // FactoryBase
    class FactoryBase {

     function 
    _GetClass$psAlias ) {
       die( 
    "blah blah, should be extended ..." );
     }

     function &
    Build$psAlias ) {
        
    $className $this->_GetClass$psAlias );
        
    // do an import of the class file here
        
    return $className::instance();
     }


    PHP Code:
    // ModelFactory
    class ModelFactory extends FactoryBase {

      function 
    ModelFactory() {}

      function 
    _GetClass$psAlias ) {
        
    // do a little switch and return the actual Class Name
        
    return "Menu";
      }

    I could do the work in the ModelFactory->_GetClass() function but that will mean that I have to duplicate my import code in each extended class.

    Any ideas or am I completely confusing myself here with this one?

    Thanks,
    Af.

  6. #6
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could always use:
    PHP Code:
    $className "Menu"
    eval(
    "\$model =& $className::instance()"

  7. #7
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    } in the wrong place.

    Quote Originally Posted by Afro Boy
    PHP Code:
    // part of the Menu class 
    function &instance() { 
        static 
    $model
        if (!isset(
    $model)) { 
            
    $model = new Menu(); 
        }
            return 
    $model

    PHP Code:
    $name 'Menu';
    $m = &call_user_func(array($name'instance')); 
    Appears to work.

  8. #8
    SitePoint Enthusiast escape164's Avatar
    Join Date
    Dec 2002
    Location
    Colorado, USA
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren
    PHP Code:
    $name 'Menu';
    $m = &call_user_func(array($name'instance')); 
    Appears to work.
    Here's an even better one, we ran into the same problem.

    PHP Code:
    function call_static_class_method() {
         
    $args array_slice(func_get_args(),1);     //Take of the first element of the array
         
    $arg func_get_arg(0);
         list(
    $class,$method) = explode('::',$arg);  //Snag out the class and method name
         
    return call_user_func_array(array($class,$method),$args);   //Call the function and return it's value
    }

    //Usage
    call_static_class_method('ClassName::MethodName',$var1,$var2,$var3); 
    A few more notes:
    I heard Eval is slow, and this function seems to work pretty fast.
    The name is surprisingly easy to remember, we've used it in a couple different projects where I forgot it was there.

    Hope that helps!!!

  9. #9
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, not sure that's an improvement. Mainly because if either class, or method are spelt incorrectly, the error thrown by PHP will indicate the problem is in the call_static_class_method() function, when infact its elsewhere. And pfaffing with debug_backtrace() to locate where the real call came from.

  10. #10
    SitePoint Enthusiast escape164's Avatar
    Join Date
    Dec 2002
    Location
    Colorado, USA
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very true.

    But this function was not meant to help w/ debugging. It was meant to be a quick & dirty alternative to instantiating a bunch of classes and using up lots of extra memory. I wrote it to be as fast as possible, (had it down to 3 lines at one point, but turned out to be unreliable). It's meant for advanced developers and usually advanced developers know enough to find the error generated by this function quickly.

  11. #11
    SitePoint Member
    Join Date
    Mar 2004
    Location
    London
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for the advice on call_user_func & call_user_func_array - my problem is 100% solved, and elegantly!
    Anyone who needs to call static class methods using a variable class name - look no further.


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
  •