"Call to Undefined Function" Error When Calling Class Method

call to undefined function error when calling class method

You dont have a function named assign(), but a method with this name. PHP is not Java and in PHP you have to make clear, if you want to call a function

assign()

or a method

$object->assign()

In your case the call to the function resides inside another method. $this always refers to the object, in which a method exists, itself.

$this->assign()

Call to undefined function in Class

The same error can be reproduced with a smaller bit of code like this:

<?php

class Foo{
public function baz(){
bar();
}

public function bar(){
echo "boo";
}
}

$foo = new Foo();

$foo->baz();

PHP Fatal error: Uncaught Error: Call to undefined function bar() in
/home/hpierce/PhpstormProjects/Temp/addNumbers.php:5

That error is talking about the reference to bar() on 5th line, stored within the class, not the method call on the Foo object:

class Foo{
public function baz(){
bar(); //<--- This!
}

//...
}

$foo->baz(); // <-- NOT this.

In PHP, referencing a method of a class from within the class requires the function call to be prefixed with $this->. Without using that PHP is attempting to use a function defined in the global namespace, where there isn't a function named sub().

This is different from languages like Java, where functions can be referenced without an explicit self reference.

The above code can be fixed like this:

<?php

class Foo{
public function baz(){
$this->bar();
}

public function bar(){
echo "boo";
}
}

$foo = new Foo();

$foo->baz();

Call to undefined function in Laravel 8

This is more of a syntax error - not so much Laravel. You need to define the Model class and call the method create while referencing your class.

The solution would be to call your Model class and then call the appropriate method.

$thread = Thread::create($request->all());

This is because there is a create() method for Laravel's models. If you do not reference the Model, PHP cannot find the proper method you are looking for.

In PHP when you call a function from within a class' method, it will look for a global or a locally defined function or throw an error as you have above. To call a method within the same class you need to use the $this reference.

For example:

class Foo {
function bar() {
$this->foobar();
}

function foobar() {
//
}
}

Now in your case you are in your controller ThreadsController[??] class, and you are calling a method from, I assume, your Thread model class. To call a method from another class object you must define the class (if non-static) and then you can call it:

class foo {
function bar() {
$bar = new bar();
$bar->foo();
}
}

class bar {
function foo() {
//
}
}

Checkout their docs for more details on the basics.

PHP Classes - Fatal error: Call to undefined method

You got the wrong direction.. if ClassB extends ClassA, ClassB inherits everything from ClassA and not the other way.. So you have to write the Code as follows:

$xxx = new BClass();
$xxx->call();
$xxx->momo();

php call to undefined function from within a class

Assure that this global file where function get_session_user_id() is defined is loaded before class Ref is loaded.

In other words class loading should go in this way:

1. Include global file which contains impl of get_session_user_id()
2. Load class Ref

To check what is going on add this inside the Ref.php before the class definition

if (!function_exists('get_session_user_id')) {
die('GLOBAL FILE NOT LOADED and get_session_user_id() function not available!!!');
}


Related Topics



Leave a reply



Submit