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();
That error is talking about the reference toPHP Fatal error: Uncaught Error: Call to undefined function bar() in
/home/hpierce/PhpstormProjects/Temp/addNumbers.php:5
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
Programmatically Create Image from Web-Page or a Single Div
Codeigniter 4 Problem Installing with Composer
How to Prevent PHP Script Running More Than Once
Regex to Conditionally Replace Twitter Hashtags with Hyperlinks
Wordpress Paginate_Links - How to Use It
Efficient Reloading Data/Pushing Data from Server to Client
How to Generate a Round Robin Tournament in PHP and MySQL
Php:Capturing the Command Output
How to Watch a File Write in PHP
Apache Virtual Host Not Parsing PHP
Remove "(Optional)" Text from Checkout Fields in Woocommerce 3.4+
How to Access My Wamp Server on Another Local Computer
Merging Overlapping Ranges in PHP Arrays
Laravel Errno 150 Foreign Key Constraint Is Incorrectly Formed
Change Mime Type of Output in PHP
Datetime Now PHP MySQL (+ Pdo Variant)
MySQL Fulltext Search Multiple Words
Difference Between $_Server['Document_Root'] and $_Server['Http_Host']