BUG: Referring to a class function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mrbog

    BUG: Referring to a class function

    This is a "namespace" problem with "::". Is there an official place I
    can report php bugs? I'm new to PHP (and soon to be leaving it):

    class SomeClass {
    function dostuff($ar) {
    uasort($ar, 'SomeClass::som esort');
    }

    function somesort($a,$b) {<-- never gets called
    echo "Yes I got called"; //<-- never happens
    if ($a[order] == $b[order]) { return 0;}
    if ($a[order] < $b[order]) { return -1;};
    if ($a[order] > $b[order]) { return 1;};
    }
    }
    SomeClass::dost uff($ar); //put whatever you want in the $ar array to
    test

    In the line "uasort($ar , 'SomeClass::som esort');",
    'SomeClass::som esort' isn't recognized by PHP. I also tried just
    'somesort', no go.

    I was trying to use a class as a substitute for PHP's lack of
    namespaces. (Which btw is the reason I'm bailing out of php.)
    Because of this bug I have to put my sorting funct into the global
    space, which is ridiculous.
  • Cameron

    #2
    Re: BUG: Referring to a class function

    mrbog wrote:[color=blue]
    > This is a "namespace" problem with "::". Is there an official place I
    > can report php bugs? I'm new to PHP (and soon to be leaving it):
    >
    > class SomeClass {
    > function dostuff($ar) {
    > uasort($ar, 'SomeClass::som esort');
    > }
    >
    > function somesort($a,$b) {<-- never gets called
    > echo "Yes I got called"; //<-- never happens
    > if ($a[order] == $b[order]) { return 0;}
    > if ($a[order] < $b[order]) { return -1;};
    > if ($a[order] > $b[order]) { return 1;};
    > }
    > }
    > SomeClass::dost uff($ar); //put whatever you want in the $ar array to
    > test
    >
    > In the line "uasort($ar , 'SomeClass::som esort');",
    > 'SomeClass::som esort' isn't recognized by PHP. I also tried just
    > 'somesort', no go.
    >
    > I was trying to use a class as a substitute for PHP's lack of
    > namespaces. (Which btw is the reason I'm bailing out of php.)
    > Because of this bug I have to put my sorting funct into the global
    > space, which is ridiculous.[/color]

    Why are you using :: rather than creating an instance of the class? i.e.

    $foo = new SomeClass;

    when you use a class function as a callback one usually uses the $this
    pointer, however as you didn't create an instance of the class then you
    can't, I don't think this is a bug, I think the problem is that you
    aren't using classes properly.

    ~Cameron

    Comment

    • Alex Farran

      #3
      Re: BUG: Referring to a class function

      mrbog writes:
      [color=blue]
      > This is a "namespace" problem with "::". Is there an official place I
      > can report php bugs? I'm new to PHP (and soon to be leaving it):[/color]

      Patience. If you let this small problem frustrate you, you'll have
      just as many problems whatever language you choose.
      [color=blue]
      > uasort($ar, 'SomeClass::som esort');[/color]
      Change this line to uasort($ar, array('SomeClas s', 'somesort') );

      --

      __o Alex Farran
      _`\<,_ Analyst / Programmer
      (_)/ (_) www.alexfarran.com

      Comment

      • root-boy

        #4
        Re: Referring to a class function

        Why not do the following:

        class SomeClass{
        function dostuff($ar)
        {
        function SomeSort($a, $b)
        {
        /* Your sorting code goes here. */
        }

        uasort($ar, 'SomeSort');
        }
        }/* End class */

        Thus nothing, but the required function will see the sorting function. Isn't
        that peachy?

        root-boy.


        Comment

        • mrbog

          #5
          Re: BUG: Referring to a class function

          Cameron <foo@bar.invali d> wrote in message news:<c0nb74$65 2$1@newsg2.svr. pol.co.uk>...[color=blue]
          >
          > Why are you using :: rather than creating an instance of the class? i.e.
          >
          > $foo = new SomeClass;
          >
          > when you use a class function as a callback one usually uses the $this
          > pointer, however as you didn't create an instance of the class then you
          > can't, I don't think this is a bug, I think the problem is that you
          > aren't using classes properly.
          >
          > ~Cameron[/color]

          It's called a static function. PHP supports referring to functions
          that way, otherwise I wouldn't have been able to do the line
          "SomeClass::dos tuff($ar);" which does work. As I said, I'm using
          classes to accomodate PHP's (terribly unfortunate) lack of namespaces.

          Comment

          • mrbog

            #6
            Re: BUG: Referring to a class function

            Alex Farran <alex@alexfarra n.com> wrote in message news:<m3ad3kll1 m.fsf@alexfarra n.com>...[color=blue]
            > mrbog writes:
            >
            > Patience. If you let this small problem frustrate you, you'll have
            > just as many problems whatever language you choose.[/color]

            Lack of namespaces is not a small problem.
            [color=blue][color=green]
            > > uasort($ar, 'SomeClass::som esort');[/color]
            > Change this line to uasort($ar, array('SomeClas s', 'somesort') );[/color]

            Thanks very much, it worked. Don't know why they can't have PHP just
            check for "::" in the name and do this for me. But thanks again.
            (Hey is there an official place I can report bugs or is this the
            place? Not that this was a bug per se, I'm just asking in general.
            Maybe I could make it a "suggestion ".)

            thanks,
            mrb

            Comment

            • Alex Farran

              #7
              Re: BUG: Referring to a class function

              mrbog writes:
              [color=blue][color=green][color=darkred]
              >> > uasort($ar, 'SomeClass::som esort');[/color]
              >> Change this line to uasort($ar, array('SomeClas s', 'somesort') );[/color][/color]
              [color=blue]
              > Thanks very much, it worked. Don't know why they can't have PHP just
              > check for "::" in the name and do this for me. But thanks again.
              > (Hey is there an official place I can report bugs or is this the
              > place? Not that this was a bug per se, I'm just asking in general.
              > Maybe I could make it a "suggestion ".)[/color]

              I expect you'll find all you need at www.php.net.

              --

              __o Alex Farran
              _`\<,_ Analyst / Programmer
              (_)/ (_) www.alexfarran.com

              Comment

              • Cameron

                #8
                Re: BUG: Referring to a class function

                mrbog wrote:[color=blue]
                > Cameron <foo@bar.invali d> wrote in message news:<c0nb74$65 2$1@newsg2.svr. pol.co.uk>...
                >[color=green]
                >>Why are you using :: rather than creating an instance of the class? i.e.
                >>
                >>$foo = new SomeClass;
                >>
                >>when you use a class function as a callback one usually uses the $this
                >>pointer, however as you didn't create an instance of the class then you
                >>can't, I don't think this is a bug, I think the problem is that you
                >>aren't using classes properly.
                >>
                >>~Cameron[/color]
                >
                >
                > It's called a static function. PHP supports referring to functions
                > that way, otherwise I wouldn't have been able to do the line
                > "SomeClass::dos tuff($ar);" which does work. As I said, I'm using
                > classes to accomodate PHP's (terribly unfortunate) lack of namespaces.[/color]

                I know what it is.

                ~Cameron

                Comment

                • Chung Leong

                  #9
                  Re: BUG: Referring to a class function

                  That a programmer should feel he cannot do without namespace is a sad
                  commentary on the break-down of our PHP community. Shared values and
                  assumptions have so diminished that we cannot function without segregating
                  ourselves into little cocoons. It's as though relativism has reached its
                  apex, and in the world in which we program, not only can we not agree what
                  ideas mean, we cannot even agree on which name is associated with with idea.

                  Uzytkownik "mrbog" <dterrors@hotma il.com> napisal w wiadomosci
                  news:cbd4bb52.0 402150914.437ca b2@posting.goog le.com...[color=blue]
                  > Alex Farran <alex@alexfarra n.com> wrote in message[/color]
                  news:<m3ad3kll1 m.fsf@alexfarra n.com>...[color=blue][color=green]
                  > > mrbog writes:
                  > >
                  > > Patience. If you let this small problem frustrate you, you'll have
                  > > just as many problems whatever language you choose.[/color]
                  >
                  > Lack of namespaces is not a small problem.
                  >[color=green][color=darkred]
                  > > > uasort($ar, 'SomeClass::som esort');[/color]
                  > > Change this line to uasort($ar, array('SomeClas s', 'somesort') );[/color]
                  >
                  > Thanks very much, it worked. Don't know why they can't have PHP just
                  > check for "::" in the name and do this for me. But thanks again.
                  > (Hey is there an official place I can report bugs or is this the
                  > place? Not that this was a bug per se, I'm just asking in general.
                  > Maybe I could make it a "suggestion ".)
                  >
                  > thanks,
                  > mrb[/color]


                  Comment

                  Working...