Browse Source

基于psr-0基础框架

chenlong 4 years ago
parent
commit
29382e31dd
5 changed files with 54 additions and 2 deletions
  1. 6 0
      .idea/vcs.xml
  2. 8 0
      App/Controller/Home/Index.php
  3. 17 0
      Core/Loader.php
  4. 17 1
      index.php
  5. 6 1
      read.me

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 8 - 0
App/Controller/Home/Index.php

@@ -0,0 +1,8 @@
+<?php
+namespace App\Controller\Home;
+
+class Index{
+    static function test(){
+        echo 'hello chuck';
+    }
+}

+ 17 - 0
Core/Loader.php

@@ -0,0 +1,17 @@
+<?php
+namespace Core;
+
+class Loader{
+    /**
+     * @param $class
+     */
+    static function autoload($class){
+        /**
+         * $class  出来的是反斜杠 App\Controller\Home\Index
+         * 需要转化一下 再包含进来
+         * 所包含的文件需要有命名空间
+         */
+        require BASEDIR.'/'.str_replace('\\','/',$class).'.php';
+    }
+
+}

+ 17 - 1
index.php

@@ -1,2 +1,18 @@
 <?php
 <?php
-echo 'hi';
+/**
+ * 定义根目录常量 /www
+ */
+define('BASEDIR',__DIR__);
+
+/**
+ * 载入自动加载
+ */
+include BASEDIR.'/Core/Loader.php';
+
+/**
+ * 自动载入类 传入方法名
+ * 当实例化或者调用不存在的类时,会回调该方法
+ */
+spl_autoload_register('\\Core\\Loader::autoload');
+
+App\Controller\Home\Index::test();

+ 6 - 1
read.me

@@ -1 +1,6 @@
-##基于psr-0规范的自定义框架,包含类的自动加载、命名空间、各种设计模式的学习。
+##基于psr-0规范的自定义框架,包含类的自动加载、命名空间、各种设计模式的学习。
+App     应用目录
+    Controller
+Core    核心目录
+    Loader.php   类的自动载入
+index.php 入口文件