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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效