在 PHP 中从空值创建默认对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8900701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating default object from empty value in PHP?
提问by Paul
I see this error only after upgrading my PHP environment to PHP 5.4 and beyond. The error points to this line of code:
仅在将我的 PHP 环境升级到 PHP 5.4 及更高版本后,我才会看到此错误。错误指向这行代码:
Error:
错误:
Creating default object from empty value
从空值创建默认对象
Code:
代码:
$res->success = false;
Do I first need to declare my $res
object?
我是否首先需要声明我的$res
对象?
回答by Michael Berkowski
Your new environment may have E_STRICT
warnings enabled in error_reporting
for PHP versions <= 5.3.x, or simply have error_reporting
set to at least E_WARNING
with PHP versions >= 5.4. That error is triggered when $res
is NULL
or not yet initialized:
您的新环境可能在PHP 版本 <= 5.3.x 中启用了E_STRICT
警告,或者只是设置为至少PHP 版本 >= 5.4。当触发了错误的或者尚未初始化:error_reporting
error_reporting
E_WARNING
$res
NULL
$res = NULL;
$res->success = false; // Warning: Creating default object from empty value
PHP will report a different error message if $res
is already initialized to some value but is not an object:
如果$res
已经初始化为某个值但不是对象,PHP 将报告不同的错误消息:
$res = 33;
$res->success = false; // Warning: Attempt to assign property of non-object
In order to comply with E_STRICT
standards prior to PHP 5.4, or the normal E_WARNING
error level in PHP >= 5.4, assuming you are trying to create a generic object and assign the property success
, you need to declare $res
as an object of stdClass
in the global namespace:
为了符合E_STRICT
PHP 5.4 之前的标准,或者E_WARNING
PHP >= 5.4 中的正常错误级别,假设您正在尝试创建一个通用对象并分配属性success
,您需要在全局命名空间中声明$res
为对象stdClass
:
$res = new \stdClass();
$res->success = false;
回答by TMS
This message has been E_STRICT
for PHP <= 5.3. Since PHP 5.4, it was unluckilly changed to E_WARNING
. Since E_WARNING
messages are useful, you don't want to disable them completely.
此消息E_STRICT
适用于 PHP <= 5.3。自 PHP 5.4 起,它不幸改为E_WARNING
. 由于E_WARNING
消息很有用,因此您不想完全禁用它们。
To get rid of this warning, you must use this code:
要消除此警告,您必须使用以下代码:
if (!isset($res))
$res = new stdClass();
$res->success = false;
This is fully equivalent replacement. It assures exactly the same thing which PHP is silently doing - unfortunatelly with warning now - implicit object creation. You should always check if the object already exists, unless you are absolutely sure that it doesn't. The code provided by Michael is no good in general, because in some contexts the object might sometimes be already defined at the same place in code, depending on circumstances.
这是完全等效的替换。它确保了 PHP 正在默默做的完全相同的事情——不幸的是现在有警告——隐式对象创建。您应该始终检查对象是否已经存在,除非您绝对确定它不存在。Michael 提供的代码一般来说并不好,因为在某些上下文中,对象有时可能已经在代码中的同一位置定义,具体取决于环境。
回答by pirs
Simply,
简单地,
$res = (object)array("success"=>false); // $res->success = bool(false);
Or you could instantiate classes with:
或者您可以使用以下方法实例化类:
$res = (object)array(); // object(stdClass) -> recommended
$res = (object)[]; // object(stdClass) -> works too
$res = new \stdClass(); // object(stdClass) -> old method
and fill values with:
并用以下值填充值:
$res->success = !!0; // bool(false)
$res->success = false; // bool(false)
$res->success = (bool)0; // bool(false)
More infos: https://www.php.net/manual/en/language.types.object.php#language.types.object.casting
更多信息:https: //www.php.net/manual/en/language.types.object.php#language.types.object.casting
回答by kodmanyagha
If you put "@" character begin of the line then PHP doesn't show any warning/notice for this line. For example:
如果在该行的开头放置“@”字符,则 PHP 不会显示该行的任何警告/通知。例如:
$unknownVar[$someStringVariable]->totalcall = 10; // shows a warning message that contains: Creating default object from empty value
For preventing this warning for this line you must put "@" character begin of the line like this:
为了防止此行出现此警告,您必须在该行的开头放置“@”字符,如下所示:
@$unknownVar[$someStringVariable]->totalcall += 10; // no problem. created a stdClass object that name is $unknownVar[$someStringVariable] and created a properti that name is totalcall, and it's default value is 0.
$unknownVar[$someStringVariable]->totalcall += 10; // you don't need to @ character anymore.
echo $unknownVar[$someStringVariable]->totalcall; // 20
I'm using this trick when developing. I don't like disable all warning messages becouse if you don't handle warnings correctly then they will become a big error in future.
我在开发时使用这个技巧。我不喜欢禁用所有警告消息,因为如果您没有正确处理警告,那么它们将来会成为一个大错误。
回答by Daniel Adenew
Try this if you have array and add objects to it.
如果您有数组并向其添加对象,请尝试此操作。
$product_details = array();
foreach ($products_in_store as $key => $objects) {
$product_details[$key] = new stdClass(); //the magic
$product_details[$key]->product_id = $objects->id;
//see new object member created on the fly without warning.
}
This sends ARRAYof Objectsfor later use~!
这将ARRAY的对象以备后用〜!
回答by Irfan
Try this:
尝试这个:
ini_set('error_reporting', E_STRICT);
回答by Kevin Stephen Biswas
I had similar problem and this seem to solve the problem. You just need to initialize the $res object to a class . Suppose here the class name is test.
我有类似的问题,这似乎解决了问题。您只需要将 $res 对象初始化为一个类。假设这里的类名是 test。
class test
{
//You can keep the class empty or declare your success variable here
}
$res = new test();
$res->success = false;
回答by Ayan Bhattacharjee
This is a warning which I faced in PHP 7, the easy fix to this is by initializing the variable before using it
这是我在 PHP 7 中遇到的警告,解决这个问题的简单方法是在使用之前初始化变量
$myObj=new \stdClass();
Once you have intialized it then you can use it for objects
一旦你初始化了它,你就可以将它用于对象
$myObj->mesg ="Welcome back - ".$c_user;
回答by Sukron Ma'mun
First think you should create object $res = new \stdClass();
then assign object with key and value thay $res->success = false;
首先认为你应该创建对象 $res = new \stdClass();
然后使用键和值分配对象 $res->success = false;
回答by Tannin
A simple way to get this error is to type (a) below, meaning to type (b)
获得此错误的一种简单方法是在下面键入 (a),意思是键入 (b)
(a)$this->my->variable
(一种)$this->my->variable
(b)$this->my_variable
(二)$this->my_variable
Trivial, but very easily overlooked and hard to spot if you are not looking for it.
微不足道,但如果您不寻找它,则很容易被忽视并且很难发现。