首頁 > 後端開發 > php教程 > Laravel 5.3 報錯:`從空值建立預設物件`

Laravel 5.3 報錯:`從空值建立預設物件`

WBOY
發布: 2016-10-11 14:23:43
原創
1605 人瀏覽過

這是代碼:

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>
登入後複製
登入後複製

錯誤訊息:

<code>    ErrorException in ActivationService.php line 53:
    Creating default object from empty value</code>
登入後複製
登入後複製

53行的位置在程式碼中有註釋,程式碼是$user->activated = true;

問題:
上面的程式碼如何解決這個錯誤?

回覆內容:

這是代碼:

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>
登入後複製
登入後複製

錯誤訊息:

<code>    ErrorException in ActivationService.php line 53:
    Creating default object from empty value</code>
登入後複製
登入後複製

53行的位置在程式碼中有註釋,程式碼是$user->activated = true;

問題:
上面的程式碼如何解決這個錯誤?

因為$activation = $this->activationRepo->getActivationByToken($token);是你透過別的拿到的一個不為null的回傳
$activation->user_id可能,也可能沒值。
然後$user = User::find($activation->user_id);的時候沒值,回傳的是null,然後你53行$user->activated = true;就給一個空去指對象了。 。 。所以。 。最簡單你就是52行下面增加

<code>if(!$user){
    return null;
}</code>
登入後複製

不過這類你可以斷點一行行看返回是啥再去找問題

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板