Home > Backend Development > PHP Tutorial > Laravel 5.3报错:`Creating default object from empty value`

Laravel 5.3报错:`Creating default object from empty value`

WBOY
Release: 2016-10-11 14:23:43
Original
1633 people have browsed it

This is the code:

ActivationService.php

<code>    <?php
    
    namespace App;
    
    
    use Illuminate\Mail\Mailer;
    use Illuminate\Mail\Message;
    
    class ActivationService
    {
    
        protected $mailer;
    
        protected $activationRepo;
    
        protected $resendAfter = 24;
    
        public function __construct(Mailer $mailer, ActivationRepository $activationRepo)
        {
            $this->mailer = $mailer;
            $this->activationRepo = $activationRepo;
        }
    
        public function sendActivationMail($user)
        {
    
            if ($user->activated || !$this->shouldSend($user)) {
                return;
            }
    
            $token = $this->activationRepo->createActivation($user);
    
            $link = route('user.activate', $token);
            $message = sprintf('Activate account <a href="%s">%s</a>', $link, $link);
    
            $this->mailer->raw($message, function (Message $m) use ($user) {
                $m->to($user->email)->subject('Activation mail');
            });
    
    
        }
    
        public function activateUser($token)
        {
            $activation = $this->activationRepo->getActivationByToken($token);
    
            if ($activation === null) {
                return null;
            }
    
            $user = User::find($activation->user_id);
            //下面这行是53行.
            $user->activated = true;
    
            $user->save();
    
            $this->activationRepo->deleteActivation($token);
    
            return $user;
    
        }
    
        private function shouldSend($user)
        {
            $activation = $this->activationRepo->getActivation($user);
            return $activation === null || strtotime($activation->created_at) + 60 * 60 * $this->resendAfter < time();
        }
    
    }</code>
Copy after login
Copy after login

Error message:

<code>    ErrorException in ActivationService.php line 53:
    Creating default object from empty value</code>
Copy after login
Copy after login

The position of line 53 is commented in the code, the code is $user->activated = true;

Question:
How does the above code solve this error?

Reply content:

This is the code:

ActivationService.php

<code>    <?php
    
    namespace App;
    
    
    use Illuminate\Mail\Mailer;
    use Illuminate\Mail\Message;
    
    class ActivationService
    {
    
        protected $mailer;
    
        protected $activationRepo;
    
        protected $resendAfter = 24;
    
        public function __construct(Mailer $mailer, ActivationRepository $activationRepo)
        {
            $this->mailer = $mailer;
            $this->activationRepo = $activationRepo;
        }
    
        public function sendActivationMail($user)
        {
    
            if ($user->activated || !$this->shouldSend($user)) {
                return;
            }
    
            $token = $this->activationRepo->createActivation($user);
    
            $link = route('user.activate', $token);
            $message = sprintf('Activate account <a href="%s">%s</a>', $link, $link);
    
            $this->mailer->raw($message, function (Message $m) use ($user) {
                $m->to($user->email)->subject('Activation mail');
            });
    
    
        }
    
        public function activateUser($token)
        {
            $activation = $this->activationRepo->getActivationByToken($token);
    
            if ($activation === null) {
                return null;
            }
    
            $user = User::find($activation->user_id);
            //下面这行是53行.
            $user->activated = true;
    
            $user->save();
    
            $this->activationRepo->deleteActivation($token);
    
            return $user;
    
        }
    
        private function shouldSend($user)
        {
            $activation = $this->activationRepo->getActivation($user);
            return $activation === null || strtotime($activation->created_at) + 60 * 60 * $this->resendAfter < time();
        }
    
    }</code>
Copy after login
Copy after login

Error message:

<code>    ErrorException in ActivationService.php line 53:
    Creating default object from empty value</code>
Copy after login
Copy after login

The position of line 53 is commented in the code, the code is $user->activated = true;

Question:
How does the above code solve this error?

Because$activation = $this->activationRepo->getActivationByToken($token);is a non-null return you get through other methods
$activation->user_idMaybe, maybe not value.
Then $user = User::find($activation->user_id); has no value and returns null, and then you give a line 53 of $user->activated = true; Sora went to point at the object. . . so. . The easiest way is to add

below line 52
<code>if(!$user){
    return null;
}</code>
Copy after login

But in this case, you can breakpoint line by line to see what is returned and then find the problem

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template