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












0















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



namespace AppHttpControllersAdmin;


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 AppHttpControllersAdminbarcodeNumberExists()




Thanks!










share|improve this question

























  • where is barcodeNumberExists function called in your code?

    – Jonathan K
    Jan 2 at 20:28











  • also in your public function generateBarcodeNumber you don't return anything. I'd just do return mt_rand(1000000000, 9999999999);

    – Brad Goldsmith
    Jan 2 at 20:43
















0















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



namespace AppHttpControllersAdmin;


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 AppHttpControllersAdminbarcodeNumberExists()




Thanks!










share|improve this question

























  • where is barcodeNumberExists function called in your code?

    – Jonathan K
    Jan 2 at 20:28











  • also in your public function generateBarcodeNumber you don't return anything. I'd just do return mt_rand(1000000000, 9999999999);

    – Brad Goldsmith
    Jan 2 at 20:43














0












0








0








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



namespace AppHttpControllersAdmin;


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 AppHttpControllersAdminbarcodeNumberExists()




Thanks!










share|improve this question
















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



namespace AppHttpControllersAdmin;


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 AppHttpControllersAdminbarcodeNumberExists()




Thanks!







php laravel class






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 0:38







xkrlaix

















asked Jan 2 at 20:07









xkrlaixxkrlaix

1141313




1141313













  • where is barcodeNumberExists function called in your code?

    – Jonathan K
    Jan 2 at 20:28











  • also in your public function generateBarcodeNumber you don't return anything. I'd just do return mt_rand(1000000000, 9999999999);

    – Brad Goldsmith
    Jan 2 at 20:43



















  • where is barcodeNumberExists function called in your code?

    – Jonathan K
    Jan 2 at 20:28











  • also in your public function generateBarcodeNumber you don't return anything. I'd just do return mt_rand(1000000000, 9999999999);

    – Brad Goldsmith
    Jan 2 at 20:43

















where is barcodeNumberExists function called in your code?

– Jonathan K
Jan 2 at 20:28





where is barcodeNumberExists function called in your code?

– Jonathan K
Jan 2 at 20:28













also in your public function generateBarcodeNumber you don't return anything. I'd just do return mt_rand(1000000000, 9999999999);

– Brad Goldsmith
Jan 2 at 20:43





also in your public function generateBarcodeNumber you don't return anything. I'd just do return mt_rand(1000000000, 9999999999);

– Brad Goldsmith
Jan 2 at 20:43












1 Answer
1






active

oldest

votes


















0














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





share|improve this answer


























  • 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.

    – xkrlaix
    Jan 3 at 0: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 3 at 8:23












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012515%2flaravel-calling-member-function-of-class-using-this-keyword-in-controller%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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





share|improve this answer


























  • 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.

    – xkrlaix
    Jan 3 at 0: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 3 at 8:23
















0














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





share|improve this answer


























  • 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.

    – xkrlaix
    Jan 3 at 0: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 3 at 8:23














0












0








0







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 8:20

























answered Jan 2 at 21:01









Hilmi Erdem KERENHilmi Erdem KEREN

1,0811123




1,0811123













  • 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.

    – xkrlaix
    Jan 3 at 0: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 3 at 8:23



















  • 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.

    – xkrlaix
    Jan 3 at 0: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 3 at 8:23

















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.

– xkrlaix
Jan 3 at 0:40





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.

– xkrlaix
Jan 3 at 0:40




1




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 3 at 8:23





@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 3 at 8:23




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012515%2flaravel-calling-member-function-of-class-using-this-keyword-in-controller%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith