require 'includes/exceptions.php';require 'includes/autoloader.php';session_start(); //$view = new view();lib::set_item('controller' , new controller());lib::get_item('controller' , lib::PERSIST_STORGE)-render();//$content = $view-finish()
<p>require 'includes/exceptions.php'; require 'includes/autoloader.php'; session_start();</p><p>//$view = new view(); lib::set_item('controller' , new controller()); lib::get_item('controller' , lib::PERSIST_STORGE)->render(); //$content = $view->finish();</p>
这一章主要是讲SPL,所以我们只看第二行的autoloader.php.当我们在index.php中加入
require 'simple/simple.php';
之后。
我们看一下autoloader.php的源码
<?php class autoloader{ public static function includesautoloader($class){ $path = defined('SIMPLE_PATH') ? SIMPLE_PATH : $_SERVER['DOCUMENT_ROOT']; $file_name = $path . '/includes/' . $class . '.php'; if(is_readable($file_name)) require $file_name; } public static function modulesautoloader($class){ $path = defined('SIMPLE_PATH') ? SIMPLE_PATH : $_SERVER['DOCUMENT_ROOT']; $file_name = $path . '/modules/' . $class . '.php'; if(is_readable($file_name)) require $file_name; } public static function controllerautoloader($class){ $path = defined('SIMPLE_PATH') ? SIMPLE_PATH : $_SERVER['DOCUMENT_ROOT']; $file_name = $path . '/controller/' . $class . '.php'; if(is_readable($file_name)) require $file_name; } } spl_autoload_register('autoloader::includesautoloader'); spl_autoload_register('autoloader::modulesautoloader'); spl_autoload_register('autoloader::controllerautoloader');
这个类都是静态方法,当引用这个PHP文件后,程序会自动载入这三个文件夹下的内容,这样我们不用重复去required 或include。如果对SPL有不了解的,可以直接去百度,这里只是告诉大家,这个SPL作用相当广。
然后在includes文件夹内新建一个lib.php
<?php class lib{ const SETTING_ARRAY = true; const PERSIST_STORGE = false; public static function set_item($name , $value , $is_array = false){ if($is_array){ $_SESSION[$name] = array(); $_SESSION[$name][] = $value; } else{ $_SESSION[$name] = $value; } } public static function get_item($name , $persist = true){ $result = null; if(isset($_SESSION[$name])){ $result = $_SESSION[$name]; } if(!$persist){ unset($_SESSION[$name]); } return $result; } public static function sendto($url = ''){ if(empty($url)){ $url = '/'; } die(header('Location:' . $url)); } }
这个类目前只用于设置SESSION相关的内容。
当执行到lib::set_item('controller' , new controller());时,这里新建了一个控制器对象,并对象存入SESSION中。
controller.php
<?php class controller{ //URL部分 protected $parts; //方法参数 protected $params; public function __construct(){ $this->parts = array(); $this->analysis(); } public function analysis(){ $path_info = $_SERVER['PATH_INFO']; if(substr($path_info , 0 , 1) == '/'){ $path_info = substr($path_info , 1); } $parts = explode('/' , $path_info); if(empty($parts[0])) $parts[0] = 'index'; if(empty($parts[1])) $parts[1] = 'demo'; $this->parts = $parts; array_shift($parts); array_shift($parts); $this->params = $parts; } public function render(){ if(!class_exists($this->parts[0])){ throw new ControllerDoesntExistsException($this->parts[0] . ' not exists!'); } if(!method_exists($this->parts[0] , $this->parts[1])){ throw new ActionDoesntExistsException($this->parts[0] . 'of ' . $this->parts[1] . ' not exists!'); } $new_controller = new $this->parts[0]; $called = call_user_func_array(array($new_controller , $this->parts[1]) , $this->params); if($called === false){ throw new ActionFailedException($this->parts[0] . 'of ' . $this->parts[1] . ' failed to excute property!'); } } }
这个类只用PATH_INFO模式来获取调用的控制器和ACTION,并将参数传递到相应的ACTION方法里。当controller初始化之后,会自动分析PATH_INFO的内容,
lib::get_item('controller' , lib::PERSIST_STORGE)->render();
然后调用render()就会在相应的控制器目录找到控制器和ACTION。
例如我在controller文件夹内新建一个index.php
<?php class index{ public function demo(){ echo "sdfsdf"; } }
那么执行http://localhost/index.php/index/demo就会输出"sdfsdf";(这是我的本地路径)。
以上代码难度不高,如果没有看懂的同学,请Q我,也可以去PHP官网上去查找相应函数的用法。
下一节相会对视图作一个简单的介绍。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版
視覺化網頁開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Linux新版
SublimeText3 Linux最新版