[英]Trying to get property of non-object from a returned object
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. 我有一个文件正在用作PHP,以用作配置文件来存储可能需要经常更改的信息。 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; 在我的课程中,我有一个构造函数来调用它: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? 有什么建议么?
In your example only $this->config
is an object. 在您的示例中,仅$this->config
是一个对象。 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: 要拥有所有对象,可以对JSON进行编码然后解码:
$this->config = json_decode(json_encode($this->config));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.