尝试从返回的对象获取非对象的属性

尝试从返回的对象获取非对象的属性

问题描述:

I have a file I am using as a PHP to act as a config file to store info that might need to be changed frequently. I return the array as an object, like so:

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"
    )
);

In my classes I have a constructor to call this: private $config;

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

And the use it like:

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

However, I am given the error:

[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

I don't understand why this is happening considering the thing is returned as an object and it seemed to work for other people, as I got this idea from another question. Any suggestions?

我有一个文件,我用作PHP作为配置文件来存储可能需要的信息 经常变化。 我将数组作为对象返回,如下所示: p>

  return(object)array(
“host”=> array(
“URL”=>“  https://thomas-smyth.co.uk“
),
 
”dbconfig“=>数组(
”DBHost“=>”localhost“,
”DBPort“=>”3306  “,
”DBUser“=>”thomassm_sqlogin“,
”DBPassword“=>”SQLLoginPassword1234“,
”DBName“=>”thomassm_CadetPortal“
),
 
”reCaptcha“=&gt  ; array(
“reCaptchaURL”=>“https://www.google.com/recaptcha/api/siteverify",
”reCaptchaSecretKey“=>”IWouldNotBeSecretIfIPostedItHere“
)
); 
   pre> 
 
 

在我的类中,我有一个构造函数来调用它: private $ config; p>

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

并使用它: p>

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

然而,我收到错误: p>

  [18-Apr-  2017 21:18:02 UTC] PHP注意:尝试在第21行的/home/thomassm/public_html/php/lib/CoreFunctions.php中获取非对象的属性
  code>  pre> 
 \  n 

我不明白为什么会发生这种情况,因为这件事作为一个对象返回,而且它似乎适用于其他人,因为我从另一个问题中得到了这个想法。 有什么建议吗? p> div>

In your example only $this->config is an object. The properties are arrays, so you would use:

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

The object looks like this:

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
        )

)

To have all objects you could JSON encode and then decode:

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