PHP Functional programming

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
keenlearner
Forum Commoner
Posts: 50
Joined: Sun Dec 03, 2006 7:19 am

PHP Functional programming

Post 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');

?>
Last edited by keenlearner on Sat Jan 12, 2008 11:07 pm, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: PHP Functional programming

Post 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().
keenlearner
Forum Commoner
Posts: 50
Joined: Sun Dec 03, 2006 7:19 am

Re: PHP Functional programming

Post 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');
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: PHP Functional programming

Post 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
keenlearner
Forum Commoner
Posts: 50
Joined: Sun Dec 03, 2006 7:19 am

Re: PHP Functional programming

Post by keenlearner »

I almost get it now, many thanks.
keenlearner
Forum Commoner
Posts: 50
Joined: Sun Dec 03, 2006 7:19 am

Re: PHP Functional programming

Post 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');

?>
Last edited by keenlearner on Sun Jan 13, 2008 12:24 am, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP Functional programming

Post by John Cartwright »

Minus the random slash below, everything looks fine

"return $funcName( $str ,\$arg2);"
keenlearner
Forum Commoner
Posts: 50
Joined: Sun Dec 03, 2006 7:19 am

Re: PHP Functional programming

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PHP Functional programming

Post by Weirdan »

you might be interested in this post: http://metapundit.net/sections/blog/166
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP Functional programming

Post 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.
Post Reply