September 10, 2013

Using Anonymous functions in php 5.2

Raj’s Question:

I know Anonymous functions are supported only in php5.3 and above.

But due to some difficult circumstances , I have to use the code below in php 5.2

Can anybody please help me to convert this to work in php 5.2

====================================================

       $fn = function($points) use ($pdf) {
        $op = 'f';
        $h = $pdf->h;
        $k = $pdf->k;
       $points_string = '';
       for($i=0; $i < 8; $i+=2){
       $points_string .= sprintf('%.2F %.2F', $points[$i]*$k,($h-$points[$i+1])*$k);
            $points_string .= $i ? ' l ' : ' m ';
       }
       $pdf->_out($points_string . $op);
    };

====================================================

The full code is available at

http://barcode-coder.com/download/php-barcode-2.0.3.zip

I have now tried for hours with create_function but somehow can get this to work.

Please help me to adapt this to php5.2

Also how to duplicate the functionality of use in php5.2

ie how to pass a var like $pdf to create_function

Just sent the variable as parameter:

function fn($points, $pdf) {
        $op = 'f';
        $h = $pdf->h;
        $k = $pdf->k;
       $points_string = '';
       for($i=0; $i < 8; $i+=2){
       $points_string .= sprintf('%.2F %.2F', $points[$i]*$k,($h-$points[$i+1])*$k);
            $points_string .= $i ? ' l ' : ' m ';
       }
       $pdf->_out($points_string . $op);
};

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!