使用下划线(PEAR样式)表示法从子文件夹自动加载php类 [英] Autoload php classes from subfolders using underscore (PEAR style) notation

查看:60
本文介绍了使用下划线(PEAR样式)表示法从子文件夹自动加载php类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对面向对象的PHP和MVC有点陌生,所以我真的需要一些帮助.

I'm a bit new to Object Oriented PHP and MVC's so I really need some help please.

我有一个MVC样式的文件夹结构,在文件系统中带有子文件夹
-例如view/classes/subfolder/classname.php

I have an MVC style folder structure with subfolders in the filesystem
- e.g. view/classes/subfolder/classname.php

我正在将mod_rewrite用于人类友好的URL,如/classname/foldername/calssname,然后将它们作为下划线分隔的值传递给页面加载器.
-例如foldername_classname

I'm using mod_rewrite for human friendly URL's, as /classname or /foldername/calssname, which are then passed to a page loader as underscore separated values.
- e.g. foldername_classname


// Page Loader File
require_once($_SERVER['DOCUMENT_ROOT'].'/local/classLoader.php');
session_start();
$page = new $_REQUEST['page'];

我以前一直在使用[if/else if/else]块在每个可能的文件夹中进行测试,但这似乎效率不高,所以我正在寻找一种更好的方法来使自动加载器在许多不同的位置显示.

I have previously been using an [if / else if / else] block to test in each possible folder but this seems inefficient, so I'm looking for a better way to have the autoloader look in many different locations.

这是我最近的失败,它无法找到所请求的任何类,仅对每个类输出一个异常,最终导致致命错误!:

Here's my latest failure, which doesn't manage to find any of the classes requested and just outputs an exception for each ending up with a fatal error!:


function classToPath($class) {
    $path = str_replace('_', '/', $class) . '.php';
    return $path;
}

function autoloadController($class) {
    echo 'LoadController'.'
'; $root = '/controller/classes/'; $pathtoclass = $root.classToPath($class); try { if( file_exists($pathtoclass) ) require_once($pathtoclass); else throw new Exception('Cannot load controller '.$class); } catch(Exception $e) { echo 'Controller exception: ' . $e->getMessage() . "
"; } } function autoloadModel($class) { echo 'LoadModel'.'
'; $root = '/model/classes/'; $pathtoclass = $root.classToPath($class); try { if( file_exists($pathtoclass) ) require_once($pathtoclass); else throw new Exception('Cannot load model '.$class); } catch(Exception $e) { echo 'Model exception: ' . $e->getMessage() . "
"; } } function autoloadView($class) { echo 'LoadView'.'
'; $root = '/view/classes/'; $pathtoclass = $root.classToPath($class); try { if( file_exists($pathtoclass) ) require_once($pathtoclass); else throw new Exception('Cannot load view '.$class); } catch(Exception $e) { echo 'View exception: ' . $e->getMessage() . "
"; } } spl_autoload_register('autoloadController'); spl_autoload_register('autoloadModel'); spl_autoload_register('autoloadView');



我还想知道到文件夹/类映射的URL应该如何工作:
-即URL:/foldername/classname mod_rewrite至foldername_classname;
foldername文件夹下的类文件名为classname.php的文件;
class foldername_classname extends another_class { etc.



I was also wondering exactly how the URL to folder/class mapping should work:
- i.e. URL: /foldername/classname mod_rewritten to foldername_classname;
with a class filename of classname.php under the foldername folder;
and a php class definition of class foldername_classname extends another_class { etc.

这是正确的方法吗?

推荐答案

偶然地,与解决我的问题的方法

所以要回答我自己的问题(并希望能帮助其他人):

So to answer my own questions (and hopefully help others):

  • 文件名应该只是URL的最后一部分
    .../not-this-part/but-this
  • 文件夹结构应该是URL结构的映射
    例如.../folder/subfolder/class
  • ,并且应将类定义为完整路径,但应使用下划线而不是正斜杠
    例如class folder_subfolder_class {
  • the filename should be just the last part of the URL
    i.e. .../not-this-part/but-this
  • the folder structure should be a map of the URL structure
    e.g. .../folder/subfolder/class
  • and the class should be defined as the full path but with underscores instead of forward slashes
    e.g. class folder_subfolder_class {

然后我为每个类系统(模型,视图和控制器)编写了一个函数,并使用spl_autoload_register()将每个函数注册为__autoload函数.因此...

I then wrote a function for each class system (model, view & controller) and used spl_autoload_register() to register each function as an __autoload function. Thus...


function loadController($class) {
    $path = $_SERVER['DOCUMENT_ROOT'].'/application/controller/';
    $filename = str_replace( '_', DIRECTORY_SEPARATOR, strtolower($class) ).'.php';

    if( file_exists($path.$filename) ) {
        require_once($path.$filename);
    }
}
etc..etc..
spl_autoload_register('loadController');
...

对于那些想知道mod_rewrite部分的人...

For those wondering about the mod_rewrite part...


IfModule mod_rewrite.c
    RewriteEngine On

    RewriteBase /

    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d

    RewriteRule ^([a-z]+)/([a-z]+)/?$ /?page=$1_$2 [QSA,NC,L]
    RewriteRule ^([a-z]+)/?$ /?page=$1 [QSA,NC,L]
/IfModule

这篇关于使用下划线(PEAR样式)表示法从子文件夹自动加载php类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆