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

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
    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();
    }
    
    点赞 评论

相关推荐