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 373697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:20:00+00:00 2026-05-12T14:20:00+00:00

I am stumped right now. In my last post about this question the answer

  • 0

I am stumped right now. In my last post about this question the answer was to use a singleton to make sure an object is only initiated 1 time but I am having the opposite problem.

If I have a file called index.php and then I include these files into it, class1.php, class2.php, class3.php, class4.php.

In index.php I will have,

<?PHP
$session = new Session();  

require_once '/includes/class1php';
require_once '/includes/class2.php';
require_once '/includes/class3.php';
require_once '/includes/class4.php';
?>

then in all 4 of the test files I will try to access a method called get() from the session class, assume the session class file is already included into the index.php page as well.

Now if I try to use…

$testvar = $session->get($var1);

in any of the test class files I will get this error

Fatal error: Call to a member function get() on a non-object

the only way the code works without an error is if I use

$session = new Session(); 

in every file.

How can I fix/avoid having to initaite the class in every file when it is already initated in the index.php file?

the goal is to let me initiate a class in 1 file like index.php and then include the class files into that page, the catch is most of the classes use methods from other classes so would be nice if I didn’t have to initiate every class in every file

  • 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-12T14:20:00+00:00Added an answer on May 12, 2026 at 2:20 pm

    Without seeing the code it’s hard to tell, but I think I can make some assumptions. correct me if I’m wrong:

    EDIT: So post your source so we can stop speculating

    1) The files you are including are class files. in other words, they contain something like:

    class a
    {
        function a(){}
    
        function b()
        {
    
        }
    }
    

    2) You aren’t trying to execute code in the class files, at load time, but at some later time by instantiating them

    i.e.

    require("class.a.php");
    
    $myA = new a();
    
    $a->b();
    

    If you are trying to reference your session variable inside those classes, then you have a scope issue. A variable declared outside a class definition can’t be used inside the class, unless it is declared as a global var inside the class.

    class a
    {
        function a(){}
    
        function willFail()
        {
            $session->doSomething(); //fails
        }    
    
        function b()
        {
            global $session;
            $session->doSomething(); //succeeds
        }
    }
    

    Even then, you probably don’t want to do that, but instead you should pass in your session as a variable if the class needs access to it:

    class a
    {
        function a(){}
    
        function b($session)
        {
            $session->doSomething(); // yay!
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This has me pretty stumped. Maybe I'm too tired right now. Rectangle rectangle =
This really has my stumped today. I'm sure its simple, but... Here is my
This one has me kind of stumped. I want to make the first word
Finding a good way to do this has stumped me for a while now:
Right now I am trying to use selenium ide to write code that makes
I am in DataGrid hell right now but that's another post. Anyway, I wrote
I am completely stumped as to why this code does not get any SDL
I'm completely stumped on this one. I have three different lists that need to
I have this strange call stack and I am stumped to understand why. It
I don't know if anyone has seen this issue before but I'm just stumped.

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.