What is the best way to organize files and folders in an object oriented PHP project? [closed]

☆樱花仙子☆ 提交于 2021-02-08 12:13:27

问题


What is the best directory structures of an object oriented PHP project? taking security into consideration among the other factors. I usually use these technologies to build websites: OOP PHP/MySql, html, css, javascript/jQuery, ajax and smarty. And I don't want to use a framework right now.


回答1:


Just use namespaces with an autoloader.

This way you can organize your folders this way:

/
  src
    Model
      User.php
    View
      index.php
    Controller
     HomeController.php
  assets
    img
    js
    css

Your User class will be like:

namespace Model;

class User{ ... }

and you can refer to it this way:

$user = new \Model\User;

or:

<?php

use \Model\User;

...
// later in your code
$user = new User;

Where's the magic? When you ask for a class, the autoloader requires it [and throws an exception if anything goes wrong].

A basic autoloader will look like:

spl_autoload_register(function ($class) {
    include 'src' . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
});

It will search classes from src folder based on class name, in this case Model\User.


These are the basics, it's up to you to tweak it a bit.




回答2:


The best way is that the directory structure and file-organizing reflects the application so that it is accessible for development and operation.



来源:https://stackoverflow.com/questions/12572302/what-is-the-best-way-to-organize-files-and-folders-in-an-object-oriented-php-pro

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!