2017-04-18 7 views
0

私はファイルとして、頻繁に変更する必要のある情報を格納するための設定ファイルとしてPHPを使用しています。私のクラスで返されたオブジェクトからnon-objectのプロパティを取得しようとしています

return (object) array(
    "host" => array(
     "URL" => "https://thomas-smyth.co.uk" 
    ), 

    "dbconfig" => array(
     "DBHost" => "localhost", 
     "DBPort" => "3306", 
     "DBUser" => "thomassm_sqlogin", 
     "DBPassword" => "SQLLoginPassword1234", 
     "DBName" => "thomassm_CadetPortal" 
    ), 

    "reCaptcha" => array(
     "reCaptchaURL" => "https://www.google.com/recaptcha/api/siteverify", 
     "reCaptchaSecretKey" => "IWouldNotBeSecretIfIPostedItHere" 
    ) 
); 

私はこれを呼び出すためのコンストラクタがあります:私はそうのように、オブジェクトとして配列を返す プライベート$の設定を、

function __construct(){ 
    $this->config = require('core.config.php'); 
} 

そして使用はそれが好き:

[18-Apr-2017 21:18:02 UTC] PHP Notice: Trying to get property of non-object in /home/thomassm/public_html/php/lib/CoreFunctions.php on line 21 

これはとして返される事を考慮起こっている理由を私は理解していない:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('secret' => $this->config->reCaptcha->reCaptchaSecretKey, 'response' => $StrToken))); 

はしかし、私はエラーを与えています私は別の質問からこの考えを得たので、オブジェクトとそれは他の人のために働くように見えました。助言がありますか?

答えて

1

あなたの例では、$this->configはオブジェクトです。プロパティが配列されているので、あなたが使用します。

$this->config->reCaptcha['reCaptchaSecretKey'] 

オブジェクトは次のようになります。

stdClass Object 
(
    [host] => Array 
     (
      [URL] => https://thomas-smyth.co.uk 
     ) 

    [dbconfig] => Array 
     (
      [DBHost] => localhost 
      [DBPort] => 3306 
      [DBUser] => thomassm_sqlogin 
      [DBPassword] => SQLLoginPassword1234 
      [DBName] => thomassm_CadetPortal 
     ) 

    [reCaptcha] => Array 
     (
      [reCaptchaURL] => https://www.google.com/recaptcha/api/siteverify 
      [reCaptchaSecretKey] => IWouldNotBeSecretIfIPostedItHere 
     ) 

) 

あなたは可能性がJSONエンコードすべてのオブジェクトを持っているし、次にデコードするには:

$this->config = json_decode(json_encode($this->config)); 
+0

何ですかstdClassのこと? –

+0

それはオブジェクトです。キャストでオブジェクトを作成したり、クラスを指定していないソースからオブジェクトを作成したりすると、そのオブジェクトは 'stdClass'クラスになります。 – AbraCadaver

関連する問題