Creating Default Object from Empty Value in PHP

Creating default object from empty value in PHP?

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:

$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 = 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:

$res = new \stdClass();
$res->success = false;

PHP Warning: Creating default object from empty value in

The error is not about $status object, but $status->about. Assign an instance of stdClass to it first and the error will go.

$status = new \stdClass();

$status->about = new \stdClass();

$status->about->requester = 'username';
$status->about->method = 'method';
$status->about->command = 'cmd';

Error message: Creating default object from empty value

You're getting this error Warning: Creating default object from empty value because $review->title returns false.

find() takes an id and returns a single model. If no matching model exist, it returns null.

You can use findOrFail() instead of find(), if the record not matched then findOrFail() will throw a 404 error :

$review = Review::findOrFail($request->id);

I think you've missed to define a parameter on your function, that passes with your route, probaby its {id} :

public function updateReview(Request $request, $id)
{
$review = Review::findOrFail($id);
//...
}

Laravel 5.8: Creating default object from empty value

When you search for existing Withdraw Wallet with an id that doesnt exist, it will return null. and since you assign 'cancelled' as an object attribute on the variable, php will convert $findRequest to a default object (StdObject::class) to be able to assign the attribute to it.

One simple solution would be to use findOrFail() instead of find() wich will trigger an exception when the id doesnt exist in your DataBase and return a 404 response on the request.

$findRequest = WithdrawWallet::findOrFail($id);
$findRequest->status = 'cancelled';
$findRequest->save();

return redirect()->back();

another suggestion (if you have soft delete on on the model and the id exists in the database) is to use withTrashed()

$findRequest = WithdrawWallet::withTrashed()->findOrFail($id);
$findRequest->status = 'cancelled';
$findRequest->save();

return redirect()->back();

PHP Array Warning: Creating default object from empty value

You get the warning

Warning: Creating default object from empty value

because you do

$searcharticles->$postid->post_id = $postid;

Your $searcharticles is a StdClass. You assign the property $postId and then immediately chain off another property post_id, which forces PHP to create a new default object.

To avoid it, put this before the chained method calls:

$searcharticles->$postid = new StdClass;
$searcharticles->$postid->post_id =$postid;
$searcharticles->$postid->title =$title;
$searcharticles->$postid->titlescore = $titlescore;
$searcharticles->$postid->quality =$quality;


Related Topics



Leave a reply



Submit