note 92759 deleted from language.oop5.basic by cmb
| From: | cmb@php.net | Date: | Mon, 21 Nov 2016 18:10:08 +0000 |
| Subject: | note 92759 deleted from language.oop5.basic by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207264@lists.php.net to get a copy of this message | ||
Note Submitter: alex c
----
ok this really basic but I always forget this. I always get an error like:
Fatal error: Call to a member function on a non-object
when i deal with oops
if it were me finding the error i'd search the internet for hours and then it would occur to
me, I'm putting my class operator inside a function, but i would define the class in global
file.
so like this:
test.php
<?
include(class.php);
$class = new newclassname;
function function1(){
$class->dofunc();
}
?>
you'll get some die errors and try and do this with function1,
function function1(){
newclassname::dofunc();
}
but if you're using $this inside your class then you'll get another error on non object
so basically, all you need to do is:
function function1(){
$class = new newclassname;
$class->dofunc();
}
or
function function1(){
global $class;
$class->dofunc();
}
i know it's simple, but it always gets me!