• Resolved batman42ca

    (@batman42ca)


    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?

Viewing 1 replies (of 1 total)
  • Thread Starter batman42ca

    (@batman42ca)

    Never mind. I just figured it out. I need to declare my object as global not just where I use it but also where I instantiated it in header.php

Viewing 1 replies (of 1 total)
  • The topic ‘Instantiate PHP class in header,.php use elsewhere’ is closed to new replies.