0

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!

rameezmeans
  • 830
  • 1
  • 10
  • 21

1 Answers1

2

$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();
}
Hilmi Erdem KEREN
  • 1,949
  • 20
  • 29
  • excellent explanation sir. so if we don't use $this before calling member function of the class then it will call the function by the namespace in PHP? by the way i edited the question and now its making sense. – rameezmeans Jan 03 '19 at 00:40
  • 1
    @xkrlaix Yes you are right. Because php supports functions that are not class members, we use `$this` to avoid conflicts and confusions. – Hilmi Erdem KEREN Jan 03 '19 at 08:23