note 92759 added to language.oop5.basic
| From: | alexc at osu1 dot php dot net | Date: | Fri, 07 Aug 2009 19:17:44 +0000 |
| Subject: | note 92759 added to language.oop5.basic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159194@lists.php.net to get a copy of this message | ||
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!
----
Server IP: 69.147.83.197
Probable Submitter: 69.55.78.34
----
Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php
Edit -- https://master.php.net/note/edit/92759
Del: integrated -- https://master.php.net/note/delete/92759/integrated
Del: useless -- https://master.php.net/note/delete/92759/useless
Del: bad code -- https://master.php.net/note/delete/92759/bad+code
Del: spam -- https://master.php.net/note/delete/92759/spam
Del: non-english -- https://master.php.net/note/delete/92759/non-english
Del: in docs -- https://master.php.net/note/delete/92759/in+docs
Del: other reasons-- https://master.php.net/note/delete/92759
Reject -- https://master.php.net/note/reject/92759
Search -- https://master.php.net/manage/user-notes.php