Jump to content

Recommended Posts

I need to to a little functional programming, my question is in the code. Thanks.

 

<?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.
class obj {
   function __construct(){}
}
$obj = new obj();
$printFunction = applyOne(array($obj), 'print2');
$printFunction('arg2 string');

?>

 

Can I have the array that has been passed to stay alive until passed to the print function ?

Link to comment
https://forums.phpfreaks.com/topic/85772-php-functional-programming/
Share on other sites

Thanks for the reply, but sorry that is not what I mean. In short, I want to get the function print2 executed. I need this line to be completed. But not sure how to implement that, thanks.

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

 

It's so difficult in PHP in contrast to Javascript.

If you want to return something from the $funcName, then you have to return the result from inside the function, and then apply it to a variable for further use. Maybe I don't fully understand, but looking at your line of code, that was what came to mind. When I get stuck, I go back to basics. Check this out:

 

<?php
function getPath(){
$path = getcwd();
return $path;
}

echo "Your Absoluthe Path is: ";
$whatever = getPath();
echo $whatever;
?>

 

 

I think my question is not very clear, but really appreciate of your help. I think maybe this is the solution.  The key is the var_export

Or any other advice for me ? Many thanks.

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

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.