mardi 28 juillet 2015

How to pass defined object into function?

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.




Aucun commentaire:

Enregistrer un commentaire