如何从PHP中的另一个文件调用函数(require_once并调用它,不工作)

时间:2013-01-06 01:49:21

标签: php

我在从另一个文件调用函数时遇到问题。我已经尝试将require_once( $library_path .'Main/SystemUtility.class.php');放在顶部,而不是调用函数,但它不起作用。 函数名是loadController,它位于SystemUtility.class.php。

<?php
    class Main {
        function Main() {
        }

        function getInstance() {
            static $instance = null;

            if (null === $instance) {
                $instance = new Main();
            }

            return $instance;
        }

        function dispatch($args = '') {
            $page = '';
            $params = '';
            $controller = 'index';
            $action = 'index';
            $count = 0;

            if ($args != '') {
                $page = $args;
            } else {
                if (isset( $_GET['page'] )) {
                    $page = $_GET['page'];
                } else {
                    if (!isset( $_GET['page'] )) {
                        $page = 'index/index';
                    }
                }
            }


            if ($page == 'index.php') {
                $page = '';
            }

            $params = explode( '/', $page );
            $count = count( $params );

            if (0 < $count) {
                if ($params[0] != '') {
                    $controller = $params[0];
                }
            }


            if (1 < $count) {
                if ($params[1] != '') {
                    $action = $params[1];
                }
            }

            $action = str_replace( '-', '_', $action );
            $controller = str_replace( '-', '_', $controller );
            $valueparams = array(  );
            $i = 0;

            while ($i < $count - 2) {
                $valueparams[$i] = $params[$i + 2];
                ++$i;
            }

            include('script.inc.php');
            require_once($library_path.'Main/NesoteController.class.php');
            loadController($controller.'Controller');
            $controllerInstance = createControllerInstance( $controller . 'Controller' );
            $actionexists = 1;
            $actionMethod = $action . 'Action';

            if (!method_exists( $controllerInstance, $actionMethod )) {
                $actionexists = 0;

                if (!file_exists( '' . $view_path . $controller . '/' . $action . '.tpl.html' )) {
                    $err_msg = '<br><strong>Error: </strong>Requested page was not found!';
                    $ini_error_status = ini_get( 'error_reporting' );

                    if ($ini_error_status != 0) {
                        $err_msg .= '' . '<br><strong>Details: </strong><strong>' . $action . '</strong> Action not found!';
                    }

                    loadErrorTemplate( $err_msg );
                    exit( 0 );
                }
            }
            if (strcasecmp( get_parent_class( $controllerInstance ), 'NesoteController' ) != 0) {
                $ini_error_status = ini_get( 'error_reporting' );

                if ($ini_error_status != 0) {
                    echo '' . '<br><strong>Error: ' . $controller . 'Controller</strong> should extend NesoteController.';
                }

                exit( 0 );
            }

            $shownoviewerror = 0;

            if (!file_exists( '' . $view_path . $controller . '/' . $action . '.tpl.html' )) {
                $shownoviewerror = 1;
                loadTemplate( '' . $view_path . $controller . '/' . $action . '.tpl.html', '' );
            } else {
                loadTemplate( '' . $view_path . $controller . '/' . $action . '.tpl.html' );
            }

            arguments( $valueparams );

            if ($actionexists != 0) {
                $actionMethod(  );
            }


            if ($shownoviewerror == 1) {
                $ini_error_status = ini_get( 'error_reporting' );

                if ($ini_error_status != 0) {
                    echo '' . '<br><strong>Error: </strong> View file for the ' . $action . ' action, <strong>' . $view_path . $controller . '/' . $action . '.tpl.html</strong> does not exist.';
                }

                exit( 0 );
            }

            includePage( $controllerInstance );
        }
    }

    require_once( 'script.inc.php' );
    require_once( $library_path .'Main/SystemUtility.class.php');
?>

1 个答案:

答案 0 :(得分:1)

您必须创建SystemUtility的实例并在其上调用您的方法。

替换:

 include('script.inc.php');
 require_once($library_path.'Main/NesoteController.class.php');
 loadController($controller.'Controller');

使用:

 require_once($library_path.'Main/SystemUtility.class.php');
 $sysUtil = new SystemUtility();
 $sysUtil->loadController($controller.'Controller');