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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:17:39+00:00 2026-06-04T04:17:39+00:00

I’m trying to work on project using a proprietary mvc and we have a

  • 0

I’m trying to work on project using a proprietary mvc and we have a file called global.php which has code similar to this in it…

<?php
session_start();

require('config/config.php');
require('classes/pdo_extender.php');
require('classes/mainClass.php');
require('classes/utilities.php');

$mainClass = new mainClass;
?>

Then we have a page in the root directory that has the following code

<?php 
require_once($_SERVER['DOCUMENT_ROOT'].'/globals/global.php');
$mainClass->init();
?>

The code in the init function just includes a file based on the name of the current viewed page…

public function init() {
    $section=explode("/",$_SERVER['SCRIPT_NAME']);
    $section=explode(".",$section[count($section)-1]);
    include("controllers/".$section[0].".php");
}

So say we have in the root login.php, it includes global.php and calls the init function with out me having to redeclare $mainClass, so it then includes controllers/login.php but on this page now I have to redeclare $mainClass = new mainClass;

Is there anyway to get it so the files included from the init function still have access to the initial $mainClass set in global.php?

EDIT: Another solution I found in addition to the accepted is as follows:

public function init() {
    $section=explode("/",$_SERVER['SCRIPT_NAME']);
    $section=explode(".",$section[count($section)-1]);
    $mainClass= $this;
    include("controllers/".$section[0].".php");
}
  • 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-04T04:17:40+00:00Added an answer on June 4, 2026 at 4:17 am

    I had something like this come up once before. The issue right now is that the files included within that function inherit the function’s scope so they will not be accessible globally as you would like. A possible solution would be to use te function to determine the file names and paths and then returning them in an array of sorts or a string if it’s only one. Then doing the include outside of the function globally.

    public function init() {
        $section=explode("/",$_SERVER['SCRIPT_NAME']);
        $section=explode(".",$section[count($section)-1]);
        return "controllers/".$section[0].".php";
    }
    
    <?php 
    require_once($_SERVER['DOCUMENT_ROOT'].'/globals/global.php');
    $file = $mainClass->init();
    include($file);
    
    // now the file is in the global include tree along with global.php 
    // allowing the file to have access to w/e it is you have in there.
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... 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.