簡體   English   中英

將數組傳遞給PHP中的構造函數

[英]Pass array to constructor in PHP

我對這個問題不滿意,無法解決-只想檢查一下這里是否有人可以提供提示

我正在加載類並像這樣調用構造函數

include_once './Myaws.php';
$aws = new Myaws();
$aws->bucket = $images['config']['bucket'];

Myaws.php如下

class Myaws {

    public $bucket;    

    function __construct() {        
        $this->aws = Aws::factory('./config/aws_config.php');
    }

}

它就像一個魅力!

現在的問題

“ ./config/aws_config.php”只是一個數組,它會根據部署階段而變化-所以我想使其動態化。 這是我的工作,但沒有用

include_once './Myaws.php';
$aws = new Myaws();
$aws->bucket = $images['config']['bucket'];
$aws->config = $images['config']['awsconfig'];

在Myaws.php中,我更改了以下內容

class Myaws {

    public $bucket;    
    public $config;    

    function __construct() {        
        $this->aws = Aws::factory($this->config);
    }

}

它不起作用:(並且下面的一個也不起作用

include_once './Myaws.php';
$aws = new Myaws($images['config']['awsconfig']);
$aws->bucket = $images['config']['bucket'];

class Myaws {

    public $bucket;    

    function __construct($config) {        
        $this->aws = Aws::factory($config);
    }

}

這是非常基本的糟糕,我認為我似乎沒有得到。 誰能建議我如何使該變量$ config動態化?

我發現包含文件中的格式與傳遞數組中的格式不是100%兼容的。 還要檢查api文檔中的,他們如何使用my_profile傳遞那些憑據。

這是我用來生成配置的當前方法的示例

$aws = Aws::factory($this->getAwsConfig());


   private function getAwsConfig()
{
    return array(
        // Bootstrap the configuration file with AWS specific features
        'credentials' => array(
            'key' => $this->awsKey,
            'secret' => $this->awsSecret
        ),
        'includes' => array('_aws'),
        'services' => array(
            // All AWS clients extend from 'default_settings'. Here we are
            // overriding 'default_settings' with our default credentials and
            // providing a default region setting.
            'default_settings' => array(
                'params' => array(
                    'region' => $this->awsRegion
                )
            )
        )
    );

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM