dongxun7962 2019-01-02 20:07
浏览 433
已采纳

Laravel - 在控制器中使用$ this关键字调用类的成员函数

I have an Event class in Laravel as Controller class. Here is the namespace.

namespace App\Http\Controllers\Admin;

This is the class starting code and constructor.

class EventController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */

    public function __construct()
    {
        $this->middleware('auth');
    }

And here is the function name and definition

    function generateBarcodeNumber() {
    $number = mt_rand(1000000000, 9999999999); // better than rand()

    // call the same function if the barcode exists already
    if (barcodeNumberExists($number)) {
        return generateBarcodeNumber();
    }

    // otherwise, it's valid and can be used
    return $number;
}

function barcodeNumberExists($number) {
    // query the database and return a boolean
    // for instance, it might look like this in Laravel
    return User::whereBarcodeNumber($number)->exists();
}

I am calling this function in another function using $this keyword as

$event->slug_str = $this->generateBarcodeNumber();

And this is the error:

Call to undefined function App\Http\Controllers\Admin\barcodeNumberExists()

Thanks!

  • 写回答

1条回答 默认 最新

  • dongta5621 2019-01-02 21:01
    关注

    $this is the class instance variable. And it is not available inside static scope.

    class AcmeEvent
    {
        public slug_str;
    }
    
    class AcmeBarcodeEventGenerator 
    {
        public function generateEvent()//: AcmeEvent
        {
            $e = new AcmeEvent();
            $e->slug_str = $this->generateBarcodeNumber();
    
            return $e;
        }
    
        public function generateBarcodeNumber()//: int
        {
            return mt_rand(1000000000, 9999999999);
        }
    }
    
    $generator = new AcmeBarcodeEventGenerator();
    $e = $generator->generateEvent();
    die(var_dump($e)); // Will stop executing script and dump the event instance.
    

    If you want to use your class function (method) outside of the class scope, use it like this.

    $e = new AcmeEvent();
    $e->slug_str = (new AcmeBarcodeEventGenerator())->generateBarcodeNumber();
    
    die(var_dump($e)); // Will stop executing script and dump the event instance.
    

    See this question & answer


    It looks like you are calling a function called barcodeNumberExists. And it is not a class method call. PHP says that you are calling an undefined function. This is your problem. If it is a method name; be explicit about it. Like: $this->barcodeNumberExists(). Otherwise; php fill try to find a function inside the namespace instead of the class. Do you come from java?


    Added after the question edit.

    public function generateBarcodeNumber() {
        $number = mt_rand(1000000000, 9999999999); // better than rand()
    
        // call the same function if the barcode exists already
        if ($this->barcodeNumberExists($number)) {
            return $this->generateBarcodeNumber();
        }
    
        // otherwise, it's valid and can be used
        return $number;
    }
    
    private function barcodeNumberExists($number) {
        // query the database and return a boolean
        // for instance, it might look like this in Laravel
        return User::whereBarcodeNumber($number)->exists();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换