Defining a callback Func for preg_replace_callback(), within some class's method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jody.florian@gmail.com

    Defining a callback Func for preg_replace_callback(), within some class's method

    Hi,

    I'm trying to use preg_replace_ca llback within a method. The
    preg_replace_ca llback() & mycallback() pair will only be used by this
    method, and this method will probably only be called once in the
    object's lifetime (so there's no sense in making the callback function
    a method of the class...(is there??))

    Instead, I wanted to use create_function () to create a make-shift
    callback function within the method. (seeing as you can't define a
    function within a method). However, it needs to be able to access a
    property (or its getter) of the object, and it complains that it's not
    within the right scope to access it.

    What's the best way of doing this?

    class myclass {
    ....
    function mycallback($mat ches){
    return $this->properties[$matches[1]]
    }
    function mymethod ($subject){
    $mystr = preg_replace_ca llback(
    "!{([a-z]+)}!",
    array($this,'my callback'),
    $subject
    )
    }
    ....
    }

    This above is the only way I've found of making this work so far.
    However, mycallback() I really don't think should have the scope it
    does, i would like its definition to stay constrained to mymethod().
    any ideas would be greatly appreaciated!

    Jody

  • Chung Leong

    #2
    Re: Defining a callback Func for preg_replace_ca llback(), within some class's method

    What you're trying to do is create a closure, which can't be done in
    PHP as far as I know.

    Comment

    • d

      #3
      Re: Defining a callback Func for preg_replace_ca llback(), within some class's method

      <jody.florian@g mail.com> wrote in message
      news:1137450403 .012498.14620@g 43g2000cwa.goog legroups.com...[color=blue]
      > Hi,
      >
      > I'm trying to use preg_replace_ca llback within a method. The
      > preg_replace_ca llback() & mycallback() pair will only be used by this
      > method, and this method will probably only be called once in the
      > object's lifetime (so there's no sense in making the callback function
      > a method of the class...(is there??))
      >
      > Instead, I wanted to use create_function () to create a make-shift
      > callback function within the method. (seeing as you can't define a
      > function within a method). However, it needs to be able to access a
      > property (or its getter) of the object, and it complains that it's not
      > within the right scope to access it.
      >
      > What's the best way of doing this?
      >
      > class myclass {
      > ...
      > function mycallback($mat ches){
      > return $this->properties[$matches[1]]
      > }
      > function mymethod ($subject){
      > $mystr = preg_replace_ca llback(
      > "!{([a-z]+)}!",
      > array($this,'my callback'),
      > $subject
      > )
      > }
      > ...
      > }[/color]

      Have you tried using the create_function property? If you read the
      documentation page for preg_replace_ca llback (*ahem*), you'll see an example
      :)


      [color=blue]
      > This above is the only way I've found of making this work so far.
      > However, mycallback() I really don't think should have the scope it
      > does, i would like its definition to stay constrained to mymethod().
      > any ideas would be greatly appreaciated!
      >
      > Jody
      >[/color]


      Comment

      • jody.florian@gmail.com

        #4
        Re: Defining a callback Func for preg_replace_ca llback(), within some class's method

        index.php:
        <?php
        class myclass {
        protected $vars = array();
        function addVar($name,$v al){ $this->vars[$name] = $val; }
        function output(){
        $fileContents = file_get_conten ts("my.template ");
        $outputString = preg_replace_ca llback(
        '@{([A-Z]+)}@',
        create_function ('$matcharray', 'return
        $this->vars[$matches[1]];'),
        $fileContents);
        echo $outputString;
        } }
        $obj = new myclass();
        $obj->addVar("NAME ", "bob");
        $obj->addVar("MOBILE ", "0207 PHP RULES");
        $obj->addVar("EMAIL" , "bob@bob.co m");
        $obj->output();
        ?>

        my.template :
        <ul>
        <li>{NAME}</li>
        <li>{MOBILE}</li>
        <li>{EMAIL}</li>
        </ul>

        It produces the error:

        Fatal error: Using $this when not in object context in
        /www/thisproblem/index.php(10) : runtime-created function on line 1

        In the example at


        They're not implementing a callback function with create_function () in
        a class context, as opposed to my example above.

        I'm still quite at a loss myself. I'm hazy on my lambda calculus but
        surely there's a way round this?

        --
        J Florian

        Comment

        • jody.florian@gmail.com

          #5
          Re: Defining a callback Func for preg_replace_ca llback(), within some class's method

          example.php:
          <?php
          class myclass {
          protected $vars = array();
          function addVar($name,$v al){ $this->vars[$name] = $val; }
          function output(){
          $fileContents = file_get_conten ts("my.template ");
          $outputString = preg_replace_ca llback(
          '@{([A-Z]+)}@',
          create_function ('$matcharray', 'return
          $this->vars[$matches[1]];'),
          $fileContents);
          echo $outputString;
          } }

          $obj = new myclass();
          $obj->addVar("NAME ", "bob");
          $obj->addVar("MOBILE ", "0207 PHP RULES");
          $obj->addVar("EMAIL" , "b...@bob.com") ;
          $obj->output();
          ?>

          my.template :
          <ul>
          <li>{NAME}</li>
          <li>{MOBILE}</li>
          <li>{EMAIL}</li>
          </ul>

          It produces the error:

          Fatal error: Using $this when not in object context in
          /www/thisproblem/example.php(10) : runtime-created function on line 1

          In the example at


          They're not implementing a callback function with create_function () in
          a class context, as opposed to my example above.

          Since it's a) not wise and b) not possible to declare a function within
          a method (since it would be re-declared each time the method's used)
          I'm still quite at a loss myself

          I'm hazy on my lambda calculus but surely there's a way round this,
          other than creating a method solely as a one-off callback?

          Cheers

          --
          J Florian

          Comment

          • Chung Leong

            #6
            Re: Defining a callback Func for preg_replace_ca llback(), within some class's method

            jody.florian@gm ail.com wrote:[color=blue]
            >
            > They're not implementing a callback function with create_function () in
            > a class context, as opposed to my example above.
            >
            > Since it's a) not wise and b) not possible to declare a function within
            > a method (since it would be re-declared each time the method's used)
            > I'm still quite at a loss myself
            >
            > I'm hazy on my lambda calculus but surely there's a way round this,
            > other than creating a method solely as a one-off callback?[/color]

            Given it a rest. Like I said, PHP does not have built-in support for
            closure. It's possible to implement a closure class, of course. But
            then you end up with an additional class floating around instead of a
            method.

            If you hate having an extra method that much, just have the method call
            itself and branch based on the parameter passed. Example:

            function output($param = null){
            if($param) {
            }
            else {
            $outputString = preg_replace_ca llback( ... array($this,
            'output') ... );
            }
            }

            Comment

            • Andy Hassall

              #7
              Re: Defining a callback Func for preg_replace_ca llback(), within some class's method

              On 16 Jan 2006 17:40:31 -0800, "Chung Leong" <chernyshevsky@ hotmail.com> wrote:
              [color=blue]
              >What you're trying to do is create a closure, which can't be done in
              >PHP as far as I know.[/color]

              create_function () gets close to this.

              --
              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

              • Chung Leong

                #8
                Re: Defining a callback Func for preg_replace_ca llback(), within some class's method


                Andy Hassall wrote:[color=blue]
                > On 16 Jan 2006 17:40:31 -0800, "Chung Leong" <chernyshevsky@ hotmail.com> wrote:
                >[color=green]
                > >What you're trying to do is create a closure, which can't be done in
                > >PHP as far as I know.[/color]
                >
                > create_function () gets close to this.
                >
                > --
                > Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
                > http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool[/color]

                There is no good way to bind a reference to the function though, aside
                from (a) a variable global (b) a wrapper class plus the array($obj,
                'function') construct. PHP evidently doesn't allow lambda functions to
                have static variable, so you can't give a function a private variable.

                Comment

                • jody.florian@gmail.com

                  #9
                  Re: Defining a callback Func for preg_replace_ca llback(), within some class's method

                  >> Given it a rest

                  lol. I will. I've been convinced.

                  Thanks for everyone's help

                  Comment

                  Working...