在 phpstorm 中提示:@property-read
1、在 phpstorm 中提示:@property-read 。如图1
$setting = ConventionEmailSetting::findSettingByConventionId($params['convention_id']);
if($setting && $setting->enable_custom_server == ConventionEmailSetting::ENABLE_CUSTOM_SERVER_YES){
Yii::$app->mailer->transport = [
'class' => 'Swift_SmtpTransport',
'host' => $setting->host,
'username' => $setting->username,
'password' => $setting->password,
'port' => $setting->port ?: '25',
'encryption' => $setting->encryption ?: 'tls',
];
}
2、在 common/config/main-local.php 中
<?php
return [
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtpdm.aliyun.com',
'username' => '',
'password' => '',
'port' => '25',
'encryption' => 'tls',
]
],
],
];
3、在 Yii2 中,yii\swiftmailer\Mailer 组件中对 transport 并不是通过公开属性设置的,而是通过 setTransport() 方法设置的。而 Yii::$app->mailer->transport = […] 属于 直接设置属性方式,PHPStorm 无法静态分析出你其实是通过 magic setter 间接调用了 setter 方法。这就是它提示 @property-read $transport 的原因。调整如下,不再报错。如图2
Yii::$app->mailer->setTransport([ 'class' => 'Swift_SmtpTransport', 'host' => $setting->host, 'username' => $setting->username, 'password' => $setting->password, 'port' => $setting->port ?: '25', 'encryption' => $setting->encryption ?: 'tls', ]);

![在 Yii2 中,yii\swiftmailer\Mailer 组件中对 transport 并不是通过公开属性设置的,而是通过 setTransport() 方法设置的。而 Yii::$app->mailer->transport = [...] 属于 直接设置属性方式,PHPStorm 无法静态分析出你其实是通过 magic setter 间接调用了 setter 方法。这就是它提示 @property-read $transport 的原因。调整如下,不再报错](https://www.shuijingwanwq.com/wp-content/uploads/2025/08/2-3.jpg)
近期评论