Linz Darlington
Linz Darlington

Reputation: 671

Pass an argument through one function to another function

I'm writing a PHP Wordpress plugin where I want to be able to call the functions I've defined in other parts of my website (e.g. on page templates).

I want to be able to pass arguments to my functions like so:

// Contained on Page Template to display content

$args1 = 'Hello';
$args2 = 'Goodbye';

saySomething( $args1, $args2);

// Contained within plugin file

function saySomething ($args1, $args2){

  //echo $args1 //Test Only
  //echo $args2 //Test Only

  function sayHello () {
      echo $args1;
  }

  function sayGoodbye () {
    echo $args2;
  }
}

I've already use 'include_once' to make sure I can call functions in my plugin file. However, for some reason the sub-functions (for want of a better word!) don't seem to work. I've tried a few things, including redefining the arguments within the first function (e.g. $newargs = $args1). Any thoughts greatly appreciated.

Upvotes: 0

Views: 85

Answers (2)

Don't Panic
Don't Panic

Reputation: 41810

A couple of main things.

  1. You've defined the sayHello and sayGoodbye functions, but they are not called.
  2. sayHello and sayGoodbye refer to variables $args1 and $args2, but those variables are undefined within their scope.

Here's a way to make it work with minimal changes to your code. I changed the names of the variables to emphasize the fact that the variables in sayHello and sayGoodbye are not the same variables as the ones in saySomething.

function saySomething ($args1, $args2){

  function sayHello ($x) {   // update the function signature so that it takes an argument
      echo $x;               // use the given parameter
  }

  function sayGoodbye ($y) {
    echo $y;
  }

  // call the functions
  sayHello($args1);
  sayGoodbye($args2);
}

One other thing:

Contrary to what it looks like, sayHello and sayGoodbye are available in the same scope as saySomething. They are not defined only within the scope of that function, even though they are written there. In effect it's the same thing as writing:

function saySomething ($args1, $args2){
  sayHello($args1);
  sayGoodbye($args2);
}

function sayHello ($x) {
  echo $x;
}

function sayGoodbye ($y) {
  echo $y;
}

Upvotes: 3

jameslafferty
jameslafferty

Reputation: 2182

To use inner functions in PHP, you have to pass scope explicitly:

$args1 = 'Hello';
$args2 = 'Goodbye';

$saySomething = function() use ($args1, $args2) {
    $sayHello = function() use ($args1) {
        echo $args1;
    };
    $sayGoodbye = function() use ($args2) {
        echo $args2;
    };
    $sayHello();
    $sayGoodbye();
};

It's not like JavaScript where the scope gets passed along automamagickally:

let args1 = 'Hello';
let args2 = 'Goodbye';

let saySomething = () => {
    let sayHello = () => {
        console.log(args1);
    };
    let sayGoodbye = () => {
        console.log(args2);
    };
    sayHello();
    sayGoodbye();
};

Upvotes: 1

Related Questions