$classname::a_static_func() won't work!!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • http://www.douglassdavis.com

    $classname::a_static_func() won't work!!




    Why is it that I can do this:

    $obj= new $classname();

    But not this:

    $classname::ast aticfunc()

    I would like to call a static function when I only have the class name
    in a variable and the function name. It seems like the above code
    should work, but it doesn't. I get:

    Parse error: syntax error, unexpected T_PAAMAYIM_NEKU DOTAYIM

    What is the correct syntax to do this please?

  • Alvaro G Vicario

    #2
    Re: $classname::a_s tatic_func() won't work!!

    *** http://www.douglassdavis.com wrote/escribió (28 Aug 2005 20:25:19
    -0700):[color=blue]
    > Why is it that I can do this:
    >
    > $obj= new $classname();
    >
    > But not this:
    >
    > $classname::ast aticfunc()[/color]

    Because the second syntax is not correct:





    --
    -- Álvaro G. Vicario - Burgos, Spain
    -- http://bits.demogracia.com - Mi sitio sobre programación web
    -- Don't e-mail me your questions, post them to the group
    --

    Comment

    • http://www.douglassdavis.com

      #3
      Re: $classname::a_s tatic_func() won't work!!


      Alvaro G Vicario wrote:[color=blue]
      > *** http://www.douglassdavis.com wrote/escribió (28 Aug 2005 20:25:19
      > -0700):[color=green]
      > > Why is it that I can do this:
      > >
      > > $obj= new $classname();
      > >
      > > But not this:
      > >
      > > $classname::ast aticfunc()[/color]
      >
      > Because the second syntax is not correct:
      >
      > http://php.grn.es/manual/en/keyword....ekudotayim.php[/color]


      I am using PHP5, but thanks for that manual reference.

      A better question is, does any one know how to call a static function
      on a class that you only have the name of in a variable?

      Since this $classname::ast aticfunc() doesn't work.

      I don't think theres any reason why it couldn't work, but that's a
      conversation that the PHP designers need to have.

      How do i do this?

      Comment

      • Andy Hassall

        #4
        Re: $classname::a_s tatic_func() won't work!!

        On 29 Aug 2005 04:17:38 -0700, "http://www.douglassdav is.com"
        <doug@douglassd avis.com> wrote:
        [color=blue]
        >A better question is, does any one know how to call a static function
        >on a class that you only have the name of in a variable?
        >
        >Since this $classname::ast aticfunc() doesn't work.[/color]



        --
        Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
        http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

        Comment

        • Hero Wanders

          #5
          Re: $classname::a_s tatic_func() won't work!!

          Hello!
          [color=blue]
          > I would like to call a static function when I only have the class name
          > in a variable and the function name.
          >
          > What is the correct syntax to do this please?[/color]

          You can use this:

          $class = 'MyClass';
          $function = 'aStaticFunc';
          call_user_func( array($class, $function), 'a', 'b', 'c');

          If you have dynamic parameter lists, use call_user_func_ array().

          Greetings,
          Hero Wanders

          Comment

          • Michael Winter

            #6
            Re: $classname::a_s tatic_func() won't work!!

            On 29/08/2005 12:17, http://www.douglassdavis.com wrote:

            [snip]
            [color=blue]
            > A better question is, does any one know how to call a static function
            > on a class that you only have the name of in a variable?[/color]

            Use the function handling functions[1].

            The first argument to the call_user_func function is the callback
            pseudo-type. This means you can pass an array that specifies a method of
            either a class or an instance (examples included in documentation of
            callback type[2]).

            You could use the class/object functions[3], such as get_class_vars, to
            do a similar thing with members. However, only the class name is
            necessary in this case, and the return value is an array of all public
            members (static and otherwise).

            Mike


            [1] <URL:http://uk2.php.net/manual/en/ref.funchand.ph p>
            [2]
            <URL:http://uk2.php.net/manual/en/language.pseudo-types.php#langu age.types.callb ack>
            [3] <URL:http://uk2.php.net/manual/en/ref.classobj.ph p>

            --
            Michael Winter
            Prefix subject with [News] before replying by e-mail.

            Comment

            Working...