Instantiate PHP class in header,.php use elsewhere
-
I’m not even sure what search terms might lead to an answer to this question so I’ll just ask.
I want to include, and declare a PHP class in header.php. Then I’d like to use that class in other files like page.php or content-page.php.
I include and instantiate a class in header.php, then I attempt to use it in content-page.php but I get an error indicating that my object is not defined. Specifically “call to a member function on a non-object”
I also tried just declaring a variable in header.php and then using it elsewhere. Same problem. The variable is undefined.
I figured it had something to do with scope. In case content-page.php is actually within a function scope, I included a “global” statement just before I attempted to use the object. That doesn’t help.
Is this problem to be expected and is there some way around it?
- The topic ‘Instantiate PHP class in header,.php use elsewhere’ is closed to new replies.