7

I'm having an error when I want to open a simple page. This is the full error:

ContextErrorException: Catchable Fatal Error: Object of class __PHP_Incomplete_Class could not be converted to string in /Applications/mampstack-5.4.20-0/apache2/htdocs/engelsvandenbroecke/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Token/AbstractToken.php line 70

What I've done in my symfony project is:

  • Generate entities from database
  • Edit User Entity for security
  • Edit security.yml
  • Added two datafixtures

This is my User Entity Class:

<?php

namespace Beachteam\BeachteamBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;

/**
 * User
 *
 * @ORM\Table(name="user", uniqueConstraints={@ORM\UniqueConstraint(name="username_UNIQUE", columns={"username"})}, indexes={@ORM\Index(name="fk_users_roles_idx", columns={"role_id"})})
 * @ORM\Entity
 */
class User implements AdvancedUserInterface
{
    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=45, nullable=false)
     */
    private $username;

    /**
     * @var string
     *
     * @ORM\Column(name="password", type="string", length=60, nullable=false)
     */
    private $password;

    /**
     * @var string
     *
     * @ORM\Column(name="salt", type="string", length=30, nullable=false)
     */
    private $salt;

    /**
     * @var string
     *
     * @ORM\Column(name="firstname", type="string", length=45, nullable=false)
     */
    private $firstname;

    /**
     * @var string
     *
     * @ORM\Column(name="surname", type="string", length=45, nullable=false)
     */
    private $surname;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255, nullable=false)
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(name="token", type="string", length=45, nullable=true)
     */
    private $token;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created", type="datetime", nullable=false)
     */
    private $created;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var \Beachteam\BeachteamBundle\Entity\Role
     *
     * @ORM\ManyToOne(targetEntity="Beachteam\BeachteamBundle\Entity\Role")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="role_id", referencedColumnName="id")
     * })
     */
    private $role;

    private $plainPassword;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
    }


    /**
     * Set username
     *
     * @param string $username
     * @return User
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    /**
     * Get username
     *
     * @return string 
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * Set password
     *
     * @param string $password
     * @return User
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

    /**
     * Get password
     *
     * @return string 
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set salt
     *
     * @param string $salt
     * @return User
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;

        return $this;
    }

    /**
     * Get salt
     *
     * @return string 
     */
    public function getSalt()
    {
        return $this->salt;
    }

    /**
     * Set firstname
     *
     * @param string $firstname
     * @return User
     */
    public function setFirstname($firstname)
    {
        $this->firstname = $firstname;

        return $this;
    }

    /**
     * Get firstname
     *
     * @return string 
     */
    public function getFirstname()
    {
        return $this->firstname;
    }

    /**
     * Set surname
     *
     * @param string $surname
     * @return User
     */
    public function setSurname($surname)
    {
        $this->surname = $surname;

        return $this;
    }

    /**
     * Get surname
     *
     * @return string 
     */
    public function getSurname()
    {
        return $this->surname;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return User
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set token
     *
     * @param string $token
     * @return User
     */
    public function setToken($token)
    {
        $this->token = $token;

        return $this;
    }

    /**
     * Get token
     *
     * @return string 
     */
    public function getToken()
    {
        return $this->token;
    }

    /**
     * Set created
     *
     * @param \DateTime $created
     * @return User
     */
    public function setCreated($created)
    {
        $this->created = $created;

        return $this;
    }

    /**
     * Get created
     *
     * @return \DateTime 
     */
    public function getCreated()
    {
        return $this->created;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set role
     *
     * @param \Beachteam\BeachteamBundle\Entity\Role $role
     * @return User
     */
    public function setRoles(\Beachteam\BeachteamBundle\Entity\Role $role = null)
    {
        $this->role = $role;

        return $this;
    }

    /**
     * Get role
     *
     * @return \Beachteam\BeachteamBundle\Entity\Role 
     */
    public function getRoles()
    {
        return array($this->role->getName());
    }

    public function eraseCredentials()
    {
        $this->setPlainPassword(null);
    }

    public function getPlainPassword()
    {
        return $this->plainPassword;
    }

    public function setPlainPassword($plainPassword)
    {
        $this->plainPassword = $plainPassword;
    }

    /**
     * Implementation of AdvancedUserInterface method
     *
     * @return boolean
     */
    public function isAccountNonExpired()
    {
        return true;
    }

    /**
     * Implementation of AdvancedUserInterface method
     *
     * @return boolean
     */
    public function isAccountNonLocked()
    {
        return true;
    }

    /**
     * Implementation of AdvancedUserInterface method
     *
     * @return boolean
     */
    public function isCredentialsNonExpired()
    {
        return true;
    }

    /**
     * Implementation of AdvancedUserInterface method
     *
     * @return boolean
     */
    public function isEnabled()
    {
        return true;
    }
}

My security.yml:

    security:
encoders:
    Beachteam\BeachteamBundle\Entity\User:
        algorithm: bcrypt
        cost: 15

role_hierarchy:
    ROLE_SUPER_ADMIN: ROLE_ADMIN

providers:
    users:
        entity:
            class: BeachteamBundle:User
            property: username

firewalls:
    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false

    secured_area:
        pattern: ^/
        anonymous: ~
        form_login:
            login_path: beach_team_loginpage
            check_path: beach_team_logincheck
            username_parameter: login[username]
            password_parameter: login[password]
            always_use_default_target_path: true
            default_target_path: beach_team_adminpage
        logout:
            path:   beach_team_logout
            target: beach_team_loginpage
        remember_me:
             key:      "%secret%"
             lifetime: 31536000 # 365 days in seconds
             path:     /
             domain:   ~ # Defaults to the current domain from $_SERVER
             remember_me_parameter: remember

access_control:
    #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
nielsv
  • 6,540
  • 35
  • 111
  • 215

3 Answers3

31

For me help'd clearing cache/cookies in browser. (in browsers session was stored old version of user's entity).

unbreak
  • 1,000
  • 1
  • 16
  • 32
11

This error usually means that you try to deserialze object without loaded class for that object. So you should somehow define this class (e.g. including file with it) and then deserialize it.

Maxim Khan-Magomedov
  • 1,326
  • 12
  • 15
0

The selected answer written by Maxim Khan-Magomedov is the correct underlying reason, and for me, clearing the cache and cookies did not help. The system I'm working in is PHP 7.1.33 on CentOS 6.10.

The first and last part of the error, 'PHP Recoverable fatal error: Object of class... ...could not be converted to string in...' is from trying to cast an object into a string instead of calling a getter method like the object value is designed to be accessed by.

Example:

<?php
  class foo
  {
    function getValue()
    {
      return 'a foo';
    }
  }
  $bar = new foo;
  echo $bar->getValue(); // correct way to access an object's value
  
  //incorrect, throws error shown:
  echo 'String $bar?: '.(string)($bar);
  // Recoverable fatal error:  Object of class foo could not be converted to string
  // in /home/.../....php on line (X).

Adding in the middle part of the error '...class __PHP_Incomplete_Class ...' as Maxim Khan-Magomedov states, is from putting the object into a session variable and then switching pages without serializing it first, and then trying to cast it into a string.

Example:

Page 1:

<?php
  session_start();
  class foo
  {
    function getValue()
    {
      return 'a foo';
    }
  }
  $bar = new foo;
  $_SESSION['A_Foo']=$bar;

Page 2:

<?php
  session_start();
  echo "String $_SESSION['A_Foo']?: ".(string)($_SESSION['A_Foo']);
  // Recoverable fatal error:  Object of class __PHP_Incomplete_Class could not be
  // converted to string in /home/.../.../....php on line (X)

I figured the first part out by seeing it in something I'm working on, finding this page, clearing my cache, seeing that not work, inspecting my work and noticing the missing ->getValue(), fixing that and seeing the error go away, then researching the middle part and testing it all together to make sure I could replicate the error.

These pages helped me figure the middle part out: PHP Session with an Incomplete Object

What is an incomplete class in PHP?: https://www.quora.com/What-is-an-incomplete-class-in-PHP

PHP PHP Incomplete Class Object with my SESSION data: https://www.edureka.co/community/94958/php-php-incomplete-class-object-with-my-session-data

__PHP_Incomplete_Class Object even though class is included before session started

Other reasons for __PHP_Incomplete_Class

The __PHP_Incomplete_Class Object in PHP: https://lifemichael.com/en/the-__php_incomplete_class-object-in-php-pro/

The __PHP_Incomplete_Class Object in PHP Video: https://www.youtube.com/watch?v=rs7qUJEY3Ag