Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Class; Notice: Undefined property error

before I ask my question, I would like to say that I searched for this question, and none of the other answers helped...

Basically, in my class DemoClass, I have 4 functions, and all of them are "undefined properties"

My error:

Notice: Undefined property: DemoClass::$function in /home/content/92/10270192/html/class.php on line 46

Note: line 46 is where i do $demoClass->function...

I have a typical class setup:

class DemoClass {
    public function __construct () {
        // stuff that works and gets called
    }
    public function testFunct () {
        // one that is an "undefined property"
    }
}

I access the class as normal:

$testClass = new DemoClass();
var_dump(testClass->testFunct); // this is what is on line 46
// ^^^ This also gives me NULL, because its undefined (? i guess...)

I've never had this problem before, any suggestions? Thanks!

like image 625
mais-oui Avatar asked May 03 '26 01:05

mais-oui


2 Answers

Brackets are required when calling a function. Change it to $testClass->testFunct() instead.

like image 146
Huey Avatar answered May 04 '26 14:05

Huey


$testClass->testFunct references a variable testFunct in the class. You need to use $testClass->testFunct() to reference a function in the class.

like image 23
Nick Coons Avatar answered May 04 '26 14:05

Nick Coons



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!