dpwuvfpu52502 2012-03-14 20:41
浏览 36
已采纳

_autoload问题

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?

  • 写回答

2条回答 默认 最新

  • douwo5710 2012-03-14 20:51
    关注

    __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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号