我在OtpController中有两个控制器PrivacypolicyController和OtpContrler一个方法generateotp在那里我想在PrivacypolicyController中使用它,我正在使用trait,但我遇到了错误。这是我的密码。
class PrivacyPolicyController extends Controller {
use OtpController;
public function getCheck($phno,$app_type)
{
$authentication = authentication::select('pp_version','toc_version')
->where('phone_no',$phno and 'application_type',$app_type);
if ( !$authentication->count() )
{
$this->generateotp($phno,$app_type);
}
}
我的otpController就像这个
class OtpController extends Controller {
trait OtpController{
public function generateotp($number,$length)
{
for ($c = 0; $c < $length - 1; $c++)
{
array_push($rand, mt_rand(0, 9));
shuffle($rand);
}
return implode('', $rand);
}
}
错误为语法错误,意外的"trait"(T_trait),应为函数(T_function)
您的特征应该是一个完全不同的文件。您还可以查看关于traits的PHP文档,了解更多关于如何创建traits以及use
的位置的信息。
将其提取为文件后,您可以简单地键入:
特点:
trait OtpController {
public function generateOtp($number, $length)
{
for ($c = 0; $c < $length - 1; $c++)
{
array_push($rand, mt_rand(0, 9));
shuffle($rand);
}
return implode('', $rand);
}
}
控制器:
class OtpController extends Controller {
use OtpControllerTrait;
//
}
您可以简单地扩展要在Controller中使用其方法的Controller,而不是使用traits。