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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:08:28+00:00 2026-05-31T11:08:28+00:00

I have problem with the autoload function here is the scenario: Note: the MVC

  • 0

I have problem with the autoload function here is the scenario:

Note: the MVC framework is not my own. I’m just using it to learn more about OOP and MVC.

First, the relevant files.

.htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /phoenix/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [NE,L]
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>

index.php:

<?php require_once('./application/LOADER.php'); ?>

application/LOADER.php:

<?php

chdir(dirname(__FILE__));

require_once('./config/func_main.php');
require_once('./conf_system.php');

session_start();

ob_start('ob_gzhandler', 6);

$load = new Boot();
$load->LOAD();

?>

conf_system.php:

<?php
$C = new stdClass;
$C->INCPATH = dirname(__FILE__) . '/';

if( ! file_exists($C->INCPATH.'conf_main.php') ) {
    exit;
}
require_once($C->INCPATH.'conf_main.php');

chdir( $C->INCPATH );
?>

conf_main.php:

<?php
// Site Address Here:
$S->DOMAIN      = 'site.com';
$S->SITE_URL    = 'http://site.com/myMVC/';
?>

in config/func_main.php:

function __autoload($class_name) {
    global $C;
    require_once( $C->INCPATH.'libs/lib_'.$class_name.'.php' );
}

libs/libs_Boot.php:

class Boot {
    public function __construct() {
        $this->controller = $GLOBALS['C']->INCPATH . 'controllers/';
        $this->request = array();
    }

    public function LOAD() {
        $this->_parse_input();
        $this->_load_controller();
        $this->load_template();
    }

    private function _parse_input() {
        /* Here is the logic to get the controller name. */
        $request = explode('/', ...);
        $this->request = $request[2];
    }


    private function _load_controller() {
        require_once( $this->controller.$this->request.'.php' );
        $controller = new $this->request;
    }

    public function load_template($name) {
        global $C, $D;
        require 'view/header.php';
        require 'view/' . $name . '.php';
        require 'view/footer.php';
    }
}

controllers/index.php:

<?php $this->load_template('index');?>

In the view folder there is are only HTML files.

I know there should be an index class but I want to use functions that are in the Boot class. For example:

public function redirect($loc, $abs=FALSE) {
    global $C;
    if( ! $abs && preg_match('/^http(s)?\:\/\//', $loc) ) {
        $abs = TRUE;
    }
    if( ! $abs ) {
        if( $loc{0} != '/' ) {
            $loc = $C->SITE_URL.$loc;
        }
    }
    if( ! headers_sent() ) {
        header('Location: '.$loc);
    }
    echo '<meta http-equiv="refresh" content="0;url='.$loc.'" />';
    echo '<script type="text/javascript"> self.location = "'.$loc.'"; </script>';
    exit;
}

So that in controller/index.php I can write:

<?php
if (/* the user is not logged in */) {
    $this->redirect('signin');
} 
$this->load_template('index');
?>

Everything works finе up to a point. I get to see the view, but there is an error:

**Warning: require_once(/home/novacl/public_html/myMVC/application/libs/lib_index.php) [function.require-once]: failed to open stream: No such file or directory in /home/novacl/public_html/myMVC/application/config/func_main.php on line 6**

Line 6 is in the __autoload function.

So why this is happening? If the index controller (controller/index.php) is changed to:

class index {
    function __construct(){
        $this->load_template('index');
    }
}

I can’t use it because load_template is not a method of index, it is a method of the Boot class ..

What is happening?

  • 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-31T11:08:29+00:00Added an answer on May 31, 2026 at 11:08 am

    __autoload will be called when trying to create an instance of a class that hasn’t been defined yet. The error message indicates that somewhere the code is trying to create a class named “index”. If it’s present in the sample code, it’s probably $controller = new $this->request;. You need to include a file that defines class ‘index’ before this line. The line immediately before, require_once( $this->controller.$this->request.'.php' );, is one place to do this, as you’ve identified.

    As for this making the call $this->load_template('index') in index.php not work, you shouldn’t be doing that in any case. For one thing, the implementation of a class should be contained in a single file; otherwise, your code isn’t very cohesive. For another, $this is a free variable in the file, which is almost as bad as a global when it comes to code clarity. Instead, there should be a standard controller method that your Boot dispatcher would call; the body of load_template is a good candidate for the implementation.

    If you can’t change the controller base class (you might not have control over the framework code, but you can create your own fork, depending on the licensing), you could create an ugly hack and define an index class and call any other code you want outside of the class:

    <?php
    class index {
    }
    $this->load_template('index');
    

    However, this is terrible from a design perspective. In addition to the previous reasons (scattered implementation, free variable), it executes code in an apparent global scope that is actually function scope. Library code should only define things, not execute code directly, due to the scope confusion it can cause.

    One thing you should do is to add a call to file_exists in __autoload, to prevent trying to include a non-existent file. However, this will likely result in an “Class ‘index’ not found” fatal error unless you also apply the above fixes. Even this is preferable over the current situation, as it gives you a more relevant error.

    Note that using spl_autoload_register is recommended over defining an __autoload function, as the former allows for multiple autoload functions. The docs state that __autoload may be removed in the future.

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

Sidebar

Related Questions

Here is the exact problem. I have an autoload function set up with a
Here is my problem. I have one file (say, func.inc.php) with autoloader function, registered
I am building a custom MVC framework using PHP. My problem is when I
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have problem with fancybox. I want to write a function that will run
I have problem in my split Sentence function . The idea of ​​my function
I have this problem that I have many huge functions, and I'm using only
I have just started experiencing this problem: The site is fine until I add
I have an spl_autoload being called but the problem is the second autoload does
i have problem with autorotate on iphone i set up in all classes -

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.