preg_replace_callback, class method?

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

    preg_replace_callback, class method?

    I tried to use a class member function as a callback in
    preg_replace_ca llback, but failed. It announces: " Warning:
    preg_replace_ca llback() [function.preg-replace-callback]: requires argument
    2, 'myclass::myfun ction', to be a valid callback in ..."

    I tried both myclass::myfunc tion and $this->myfunction, neither worked. Is
    there really no way of using a class member as a callback, it has to be an
    external function? That sucks... :(



    --
    "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
    http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
    spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


  • Colin Fine

    #2
    Re: preg_replace_ca llback, class method?

    Kimmo Laine wrote:
    I tried to use a class member function as a callback in
    preg_replace_ca llback, but failed. It announces: " Warning:
    preg_replace_ca llback() [function.preg-replace-callback]: requires argument
    2, 'myclass::myfun ction', to be a valid callback in ..."
    >
    I tried both myclass::myfunc tion and $this->myfunction, neither worked. Is
    there really no way of using a class member as a callback, it has to be an
    external function? That sucks... :(
    >
    >
    >
    From


    "Some functions like call_user_func( ) or usort() accept user defined
    callback functions as a parameter. Callback functions can not only be
    simple functions but also object methods including static class methods.

    "A method of an instantiated object is passed as an array containing
    an object as the element with index 0 and a method name as the element
    with index 1.

    "Static class methods can also be passed without instantiating an object
    of that class by passing the class name instead of an object as the
    element with index 0. "

    Colin

    Comment

    • Kimmo Laine

      #3
      Re: preg_replace_ca llback, class method?

      "Colin Fine" <news@kindness. demon.co.ukwrot e in message
      news:egehjm$de9 $1$8302bc10@new s.demon.co.uk.. .
      Kimmo Laine wrote:
      >I tried to use a class member function as a callback in
      >preg_replace_c allback, but failed. It announces: " Warning:
      >preg_replace_c allback() [function.preg-replace-callback]: requires
      >argument 2, 'myclass::myfun ction', to be a valid callback in ..."
      >>
      >I tried both myclass::myfunc tion and $this->myfunction, neither worked.
      >Is there really no way of using a class member as a callback, it has to
      >be an external function? That sucks... :(
      >>
      >>
      >>
      From

      >
      "Some functions like call_user_func( ) or usort() accept user defined
      callback functions as a parameter. Callback functions can not only be
      simple functions but also object methods including static class methods.
      >
      "A method of an instantiated object is passed as an array containing an
      object as the element with index 0 and a method name as the element with
      index 1.
      >
      "Static class methods can also be passed without instantiating an object
      of that class by passing the class name instead of an object as the
      element with index 0. "
      >
      Thanks Colin!

      Altough it turned out I don't actually need this after all I found a nicer
      solution, it's good to know it works. I did try it and this works just
      perfectly:

      <?php

      class Foo {
      public function __construct(){
      echo preg_replace_ca llback(
      '/./', array(get_class ($this), 'bar'), 'lorem ipsum' );
      }

      private function bar($matches){
      return strtoupper($mat ches[0]);
      }

      }

      $my_foo = new Foo();

      ?>


      --
      "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
      http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
      spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


      Comment

      Working...