Page 1 of 1

PHP Functional programming

Posted: Sat Jan 12, 2008 11:00 pm
by keenlearner
I need to to a little functional programming, my question is in the code. Thanks.

Code: Select all

<?php
function print1($arg1, $arg2)
{
  echo 'Print 1 function : ';
  var_dump($arg1);
  var_dump($arg2);
}

function print2($arg1,$arg2)
{
  echo 'Print 2 function : ';
  var_dump($arg1);
  var_dump($arg2);
}

function applyOne($arg1,$funcName)
{
  if(is_array($arg1)) $code = "return $funcName(/*How should I write here for array ? Thanks*/ ,\$arg2)";
  else  $code = "return $funcName(\"$arg1\", \$arg2);";

  return create_function('$arg2', $code);
}

//this is working
$printFunction = applyOne('string 1', 'print1');
$printFunction('arg2 string');

//'This is not working, how to make it work for array ? But note that the array may contain other type of objects, not just string. Thanks.
$printFunction = applyOne(array('string 1'), 'print2');
$printFunction('arg2 string');

?>

Re: PHP Functional programming

Posted: Sat Jan 12, 2008 11:06 pm
by superdezign
I'm not exactly sure what you're doing, but you can recreate PHP code for a numerical array with this:

Code: Select all

$arrayString = 'array(' . implode(', ', $array) . ')';  
Or, you could simply use var_export().

Re: PHP Functional programming

Posted: Sat Jan 12, 2008 11:13 pm
by keenlearner
Thanks for the prompt reply, but will it work if the elements inside the array is not string, but is other type of objects ?
Can I have the array that has been passed to stay alive until passed to the print function ?

Code: Select all

class obj {
   function __construct(){}
}

$obj = new obj();
$printFunction = applyOne(array($obj), 'print2');
$printFunction('arg2 string');

Re: PHP Functional programming

Posted: Sat Jan 12, 2008 11:30 pm
by superdezign
Again, I'm fairly sure you can do whatever this is a different way, but just use var_export(). Read about it in the manual

Re: PHP Functional programming

Posted: Sat Jan 12, 2008 11:54 pm
by keenlearner
I almost get it now, many thanks.

Re: PHP Functional programming

Posted: Sun Jan 13, 2008 12:11 am
by keenlearner
Just to make sure, is this what you mean ? Or any other advice for me ? Many thanks.

Code: Select all

<?php

function print1($arg1, $arg2)
{
  echo 'Print 1 function : ';
  var_dump($arg1);
  var_dump($arg2);
  echo '<br>';
}

function print2($arg1,$arg2)
{
  echo 'Print 2 function : ';
  var_dump($arg1);
  var_dump($arg2);
  echo '<br>';
}

function applyOne($arg1,$funcName)
{
  if(is_array($arg1))
    {
      $str = var_export($arg1,true);
      $code = "return $funcName( $str ,\$arg2);";
    }
  else  $code = "return $funcName(\"$arg1\", \$arg2);";

  return create_function('$arg2', $code);
}

$printFunction = applyOne('string 1', 'print1');
$printFunction('arg2 string');


class obj {
  public $var1;
   function __construct(){}

   function __set_state($array)
   {
     $obj = new obj;
     $obj->var1 = $array['var1'];
     return $obj;
   }
}
$obj = new obj();
$obj->var1 = 'variable 1';
$printFunction = applyOne(array($obj), 'print2');
$printFunction('arg2 string');

?>

Re: PHP Functional programming

Posted: Sun Jan 13, 2008 12:17 am
by John Cartwright
Minus the random slash below, everything looks fine

"return $funcName( $str ,\$arg2);"

Re: PHP Functional programming

Posted: Sun Jan 13, 2008 12:31 am
by keenlearner
Thanks, but I will get error of ...if I minus the slash. I put the slash because the $arg2 has not been set, so I don't want it to be parsed and it's going to be set only when the function has been created.
Parse error: syntax error, unexpected ')' in /var/www/test.php(29) : runtime-created function on line 6

Many thanks.

Re: PHP Functional programming

Posted: Sun Jan 13, 2008 12:53 am
by Weirdan
you might be interested in this post: http://metapundit.net/sections/blog/166

Re: PHP Functional programming

Posted: Sun Jan 13, 2008 2:35 am
by John Cartwright
keenlearner wrote:Thanks, but I will get error of ...if I minus the slash. I put the slash because the $arg2 has not been set, so I don't want it to be parsed and it's going to be set only when the function has been created.
Parse error: syntax error, unexpected ')' in /var/www/test.php(29) : runtime-created function on line 6

Many thanks.
I did not realize one could escape variables with slashes, I assumed only quotes would be escaped. Typically from my experience one would user single quotes if they did not wish for the variable to be parsed.