简体   繁体   中英

Laravel - Calling member function of class using $this keyword in controller

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!

$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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM