1

I am trying to call a function from File 1 in File 2 and the referencing works. But I want a value in File 1 to pass in to the function called from File 2. For example:

function namer ($t = "hello") {
  return $t." everyone";
}
echo namer();
echo namer(null);
echo namer("There's");

// RETURNS
hello everyone
 everyone
There's everyone

So $t is a value in File 1 that I can include in value returned to File 2.

I am using AWS DynamoDB and creating a client in File 1 that I want referenced in File 2. I am using the latest AWS PHP SDK - 3.2.1.

Example:

//  FILE 1

require 'aws/aws-autoloader.php';
date_default_timezone_set('America/New_York');

use Aws\DynamoDb\DynamoDbClient;

$client = DynamoDbClient::factory(array(
  'profile' => 'default',
  'region'  => 'us-east-1',
  'version' => 'latest'
));

function showResult($c = $client, $tableName) {
  $result = $c->describeTable(array(
    'TableName' => $tableName
  ));
  return $result;
}

//  FILE 2

include 'file1.php';

echo showResult('myTable');

// This returns a Parse Error

I also tried:

function showResult($tableName) {
    $result = $client->describeTable(array(
        'TableName' => $tableName
    ));
    return $result;
}
// This returns 'Call to a member function describeTable() on a non-object'

I think this is because the inside of the function doesn't know what $client is.

Thank you.

putipong
  • 13
  • 4
  • you can eighter do `global $client;` inside function `showResult` (second version) – Jeff Jul 28 '15 at 14:24
  • or call your function (the first version) like so: `echo showResult($client, 'myTable');`. BUT change the function definition to `function showResult($c, $tableName) { ...` – Jeff Jul 28 '15 at 14:26
  • in `function showResult($c = $client, $tableName)` change the order of the parameters.... optional parameter have to come after required ones.. also you can't set the default of an option parameter to a variable. – Orangepill Jul 28 '15 at 14:26
  • That still gives me a parse error. Thats good to know for strings though. I can do `function showResult($client, $tableName = 'myTable');` and then pass the `$client` but I have to create it in `File 2` – putipong Jul 28 '15 at 14:29
  • Is it because the function doesn't know what `$client` is in the `function ({in here})`? – putipong Jul 28 '15 at 14:30

1 Answers1

1

I would recommend calling getClient like:

function getClient () {
    $client = DynamoDbClient::factory(array(
        'profile' => 'default',
        'region'  => 'us-east-1',
        'version' => 'latest'
    ));
    return $client;
}

This way all your following functions can call it like:

$result = getClient()->describeTable(array(
    'TableName' => $tableName
));

Answer

FILE 1

<?php
    require 'aws/aws-autoloader.php';
    date_default_timezone_set('America/New_York');

    use Aws\DynamoDb\DynamoDbClient;

    function getClient () {
        $client = DynamoDbClient::factory(array(
            'profile' => 'default',
            'region'  => 'us-east-1',
            'version' => 'latest'
        ));
        return $client;
    }

    function showResult($tableName) {
        $result = getClient()->describeTable(array(
            'TableName' => $tableName
        ));
        return $result;
    }
?>

FILE 2

<?php
    include 'file1.php';

    echo showResult('[Your_Table]');
?>

Reference Creating a Client for more info.

iSkore
  • 7,394
  • 3
  • 34
  • 59