public static function userIdentity(): IdentityInterface|null
{
if (self::isGuest())
{
return null;
}
$identity = \Yii::$app->user->identity;
if ($identity instanceof IdentityInterface)
{
return $identity;
}
// Defensive fallback: If identity is not instance, treat as guest
return null;
}
trying to use in the baseController
code
class BaseController extends Controller
{
private ?IdentityInterface $_isUser = null;
public function init()
{
parent::init();
$this->_isUser = RoleHelper::userIdentity();
}
}
TypeError
Cannot assign app\models\User to property app\controllers\BaseController::$_isUser of type app\controllers\IdentityInterface
Assistant
User
are all vars passed into yii2 construct $config array public?
Assistant
User
yii2
Assistant
Yes, in Yii 2, the $config array passed to the constructors of most Yii components (which usually extend from yii\base\Component) is used to initialize the object by setting its public properties.
How it works:
The constructor accepts an optional array $config = [].
It calls Yii::configure($this, $config) internally, which sets properties on $this using the key-value pairs in $config.
Only public properties can be set this way by default.
The error message:
code
TypeError
Cannot assign app\models\User to property app\controllers\BaseController::$_isUser of type app\controllers\IdentityInterface
indicates that PHP is expecting a property of type app\controllers\IdentityInterface (according to the declared type in the BaseController), but it got an instance of app\models\User instead.
What went wrong?
The property $_isUser is declared as IdentityInterface from the namespace app\controllers.
However, the user identity object returned ($identity = Yii::$app->user->identity) is an instance of your app\models\User class.
Your app\models\User class implements an interface, but PHP is treating the interface referenced in the property as a different class (a different IdentityInterface), not the interface implemented by app\models\User.
Likely cause
You imported (or implicitly referenced) IdentityInterface from the wrong namespace in BaseController.
The interface implemented by your user model (app\models\User) is probably yii\web\IdentityInterface.
But your property declares the interface as app\controllers\IdentityInterface (or some other namespace) instead.
Solution
Make sure to:
Import the correct interface in BaseController:
Add at the top of your BaseController.php:
php
use yii\web\IdentityInterface;
Declare the property with the correct interface:
php
private ?IdentityInterface $_isUser = null;
Ensure your RoleHelper::userIdentity() method returns yii\web\IdentityInterface|null.
So your BaseController should look like:
php
<?php
namespace app\controllers;
use yii\web\Controller;
use yii\web\IdentityInterface; // <- import correct interface
class BaseController extends Controller
{
private ?IdentityInterface $_isUser = null;
public function init()
{
parent::init();
$this->_isUser = RoleHelper::userIdentity();
}
}
Additional tips
Make sure that your app\models\User class implements yii\web\IdentityInterface.
If RoleHelper is in a different namespace and you want to type hint on userIdentity() method, ensure the return type is ?yii\web\IdentityInterface.
Alternatively, add full class names or imports consistently to avoid namespace conflicts.
Summary: The error is caused by a namespace mismatch where the declared interface type is assumed to be app\controllers\IdentityInterface instead of the actual yii\web\IdentityInterface implemented by the user class. Correct namespace imports resolve this issue.
In Yii2, the constructor pattern commonly used in framework classes follows this signature:
php
public function __construct(array $config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
What happens with $config?
The $config array is passed to the constructor.
The Yii::configure() method sets the properties of the object based on the keys and values in $config.
This means any public properties whose names match keys in $config get assigned the given values.
Important Points:
Only public properties can be set via the $config array by default
Yii::configure() works like this internally:
It loops through $config keys and, for each key, it sets the corresponding property if it is publicly accessible.
php
foreach ($config as $name => $value) {
if (property_exists($object, $name) && (new \ReflectionProperty($object, $name))->isPublic()) {
$object->$name = $value;
}
}
Private or protected properties won't be assigned
If you have a private or protected property, it won't be set via the $config array because those aren't accessible from outside.
Properties with public setters can be set
If the class uses PHP's magic __set() method or has public setters, the values might be assigned based on those. But by default, Yii::configure() works only with public properties.
Summary:
The $config array passed into a Yii2 constructor is a set of key-value pairs intended to initialize the object.
Yes, the keys in $config correspond to public properties (or those accessible via __set()) of the class the object is instantiated from.
Private/protected properties are not set automatically from $config.
Example
php
class MyComponent extends \yii\base\Component
{
public $username;
private $password;
}
$newObj = new MyComponent([
'username' => 'user1',
'password' => 'secret', // this will be ignored if password is private
]);
echo $newObj->username; // 'user1'
// $password is still not set because it's private and cannot be set via config array
If you want to initialize protected/private properties, you'd either:
Provide public setters
Initialize them differently, not via $config
In short: All variables passed in via the $config array are used to set public properties of the instantiated class. Private or protected properties do not get automatically configured this way.
Private and protected properties are not set from $config because they are not accessible for external assignment.
If the component defines a __set() method or exposes public setters, the properties might be set via those mechanisms, but the default Yii::configure() only assigns to public properties directly.
Example:
php
class MyComponent extends \yii\base\Component
{
public $name;
protected $secret;
public function __set($name, $value)
{
// custom setter logic
if ($name === 'secret') {
$this->secret = $value;
} else {
parent::__set($name, $value);
}
}
}
$obj = new MyComponent(['name' => 'John', 'secret' => 'xyz']);
echo $obj->name; // John
// $obj->secret = 'xyz' because of __set
Summary
$config array configures public properties by default.
Private/protected properties require either a setter or other logic to be set.
This behavior allows easy initialization of Yii objects without writing explicit setters.