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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:50:18+00:00 2026-06-08T22:50:18+00:00

I am new around here and I have a quick question if anyone can

  • 0

I am new around here and I have a quick question if anyone can help me. To keep it short I am working on a website and I have my files separated by directories and I include the files depending on the name of the page I am on. For example I have a main page dashboard.php that looks something like :

include $file1;
include $file2;
include $file3 etc.

Now I trying to use a class page that include all the files needed automatically when I construct the object.
My problem is when I include the files with the help of the class I can’t seem to find the instance of the object in my included files.
For example : $page=new Page("Test") includes $file1,$file2 etc…. Now when I am trying to use object $page in $file1 it doesn’t fiind it.
If anyone has a solution for this problem or if you think that is a wrong way to design web pages please let me know.

It’s not a problem of includes, the includes are working, I am using autoload and all that the problem is seeing objects methods in classes included through that class. Let me be more explicit :

main.php :

    $page=new Page("Test");

Page.class.php :
__construct($title)
{
include($file1);
include($file2);
}
$file1:
echo "test"; // working
$file2:
echo $page->getTitle() // doesn't work, don't worry about the method it exists and works
  • 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-06-08T22:50:20+00:00Added an answer on June 8, 2026 at 10:50 pm

    Per the manual, the include() function inherits the variable scope of the line it’s included on.

    I’m assuming you’re currently doing something like:

    class Page {
    public __construct() {
        include($file1);
        include($file2);
    }
    }
    

    If you need the file(s) to be accessible outside of the class you want them to be included in, you should add the includes outside of the class, such as:

    include($file1);
    include($file2);
    class Page { ...
    

    In the event you need the included files to have access to the class-instance itself (per your latest update), you would use the $this pseudo-variable. The reason you can’t use $page inside the included files is because $page hasn’t been defined; the scope of that variable is inside your main.php file and any other files it includes (but no “inside” classes).

    You can update your include ($file2) to use $this with echo $this->getTitle() instead of echo $page->getTitle() and it should work fine.

    Alternatively, if you would like to use the $page variable instead of $this, you can define it such as $page = $this; right before you include a file that uses it like this:

    public __construct() {
        $page = $this;
        include($file2); // file2 will now have access to a variable named $page
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new around here and i have a little problems with a C# application.
Hope you can have a quick look at what I'm doing here. Essentially, am
I have a question and i suppose this is trivial for most around here.
I'm basically brand new to LINQ. I've looked around a lot on here and
Completely new to java and I have been playing around with regex in a
I was playing around with the new CSS3 transitions and I seem to have
I have a quick question regarding memory management that I am not quite sure
I have read some questions on this around here but my tries at the
I've looked around here but can't seem to find an answer to my problem.
I'm new to AutoMapper and I've been reading and reading questions around here but

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.