Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5964431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:25:52+00:00 2026-05-22T19:25:52+00:00

I am having an unexpected issue with scope. The include documentation (also applies to

  • 0

I am having an unexpected issue with scope. The include documentation (also applies to require_once) says the required file should have access to all variable at the line it was required.

For some reason I am not able to access a class instantiated with global scope inside a function that was required in.

Would anyone know why? I am obviously missing something.

I got it working through a reference to $GLOBALS[], but I still want to know why it is not working.

UPDATE:

The error I am getting is:

Fatal error: Call to a member function isAdmin() on a non-object in <path>.php on <line>

Code:

$newClass = new myClass();

require_once("path to my file");

----- inside required file -----
function someFunction() {
     $newClass->someMethod(); // gives fatal error. (see above).
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:25:53+00:00Added an answer on May 22, 2026 at 7:25 pm

    Functions define a new scope, so inside a function you cannot access variables in the global scope.

    Variable Scope

    within user-defined functions a local
    function scope is introduced. Any
    variable used inside a function is by
    default limited to the local function
    scope

    About included files, the manual states:

    When a file is included, the code it
    contains inherits the variable scope
    of the line on which the include
    occurs.

    So if you include something in a function, the included file’s scope will be that of the function’s.

    UPDATE: Looking at your code example edited into the question, global $newClass; as the first line of the function should make it working.

    $newClass = new myClass();
    require_once("path to my file");
    
    ----- inside required file -----
    function someFunction() {
         global $newClass;
         $newClass->someMethod(); 
    }
    

    Be aware though that using global can quickly make your code more difficult to maintain. Don’t rely on the global scope, you can pass the object to the function as a parameter, or use a Singleton/Registry class (some tend to argue against the latter, but depending on the case it can be a cleaner solution).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem compiling my Java program: \Desktop\Java Programming\JFrameWithPanel.java:79: unexpected type required: variable
I'm having a strange issue here, i have this bit of code: <?php if
I am having an unexpected issue while submitting a from. The action of the
I'm having an issue with one of my scripts, the unexpected $end one. Interestingly,
Having some issue with my function call please. I have a situation that I
I have come across a large issue that i am having that has suddenly
I'm having an issue with some of my code executing in an unexpected order.
I am having an issue in someone else's code. I have code in one
Having this route: map.foo 'foo/*path', :controller => 'foo', :action => 'index' I have the
Having difficulty articulating this correlated subquery. I have two tables fictitious tables, foo and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.