Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将以前定义的对象传递到函数参数中?_Php_Amazon Web Services_Amazon Dynamodb - Fatal编程技术网

Php 如何将以前定义的对象传递到函数参数中?

Php 如何将以前定义的对象传递到函数参数中?,php,amazon-web-services,amazon-dynamodb,Php,Amazon Web Services,Amazon Dynamodb,我正试图从文件2中的文件1调用一个函数,并且引用工作正常。但是我希望文件1中的一个值传递给从文件2调用的函数。例如: function namer ($t = "hello") { return $t." everyone"; } echo namer(); echo namer(null); echo namer("There's"); // RETURNS hello everyone everyone There's everyone 因此,$t是文件1中的一个值,我可以将它包

我正试图从
文件2
中的
文件1
调用一个函数,并且引用工作正常。但是我希望
文件1
中的一个值传递给从
文件2
调用的函数。例如:

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

// RETURNS
hello everyone
 everyone
There's everyone

因此,
$t
文件1
中的一个值,我可以将它包含在返回到
文件2
的值中

我正在使用AWS DynamoDB,并在
文件1
中创建一个客户端,希望在
文件2中引用该客户端。我用的是最新的

例如:

//  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;
}

我还尝试:

function showResult($tableName) {
    $result = $client->describeTable(array(
        'TableName' => $tableName
    ));
    return $result;
}
// This returns 'Call to a member function describeTable() on a non-object'
我认为这是因为函数内部不知道
$client
是什么

谢谢。

我建议调用getClient,如下所示:

function getClient () {
    $client = DynamoDbClient::factory(array(
        'profile' => 'default',
        'region'  => 'us-east-1',
        'version' => 'latest'
    ));
    return $client;
}
这样,以下所有函数都可以像这样调用它:

$result = getClient()->describeTable(array(
    'TableName' => $tableName
));
答复
文件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;
    }
?>

更多信息请参考。

您可以选择R do
global$client内部函数
showResult
(第二个版本)或调用函数(第一个版本),如下所示:
echo showResult($client,'myTable')。但是在
函数showResult($c=$client,$tableName)中将函数定义更改为
函数showResult($c,$tableName){…
更改参数的顺序…可选参数必须在必需参数之后。此外,您不能将选项参数的默认值设置为变量。这仍然会给我一个解析错误。不过,这对于字符串来说是个好消息。我可以执行
函数showResult($client,$tableName='myTable'));
然后传递
$client
但是我必须在
文件2中创建它,是因为函数不知道
函数({in here})中的
$client
是什么?
<?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;
    }
?>
<?php
    include 'file1.php';

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