在编写php ,什么样的代码 大家更容易看懂,更高端、更装逼
microtime(true);
memory_get_usage() 内存使用情况
define()
const
DIRECTORY_SEPARATOR 目录
dirname()
basename()
define('IS_CLI', PHP_SAPI == 'cli' ? true : false);
define('IS_WIN', strpos(PHP_OS, 'WIN') !== false);
get_declared_classes(); 返回由已定义类的名字所组成的数组
sandir(__DIR__);
config::load($filename,"database");
$_arr=[]
echo "日志信息:".var_export(["say","大家好"],true); //以字符串的形式返回
日志信息:array (
0 => 'say',
1 => '大家好',
)
BIND_MODULE && Route::bind(BIND_MODULE); 记得这样的语句后面不能写 return
//获取一个类的方法 是否公开,是否static 获取一个方法的相关信息。
if (method_exists($className, 'invoke')) {
$method = new \ReflectionMethod($className, 'invoke');
if ($method->isPublic() && $method->isStatic()) {
return $className::invoke(Request::instance());
}
}
$result = method_exists($className, 'instance') ?
$className::instance() :
new $className;
//获取一个function的相关信息
$reflect = new \ReflectionFunction($function);
$args = self::bindParams($reflect, $vars);
//如果设置了缓存,如何进行header头的设置?
if (200 == $this->code) {
$cache = Request::instance()->getCache();
if ($cache) {
$this->header['Cache-Control'] = 'max-age=' . $cache[1] . ',must-revalidate';
$this->header['Last-Modified'] = gmdate('D, d M Y H:i:s') . ' GMT';
$this->header['Expires'] = gmdate('D, d M Y H:i:s', $_SERVER['REQUEST_TIME'] + $cache[1]) . ' GMT';
Cache::tag($cache[2])->set($cache[0], [$data, $this->header], $cache[1]);
}
}
判断header 有没有发送,如果没有发送,添加下面信息
if (!headers_sent() && !empty($this->header)) {
// 发送状态码
http_response_code($this->code);
// 发送头部信息
foreach ($this->header as $name => $val) {
if (is_null($val)) {
header($name);
} else {
header($name . ':' . $val);
}
}
}
APP_PATH=dirname($_SERVER['SCRIPT_FILEnAME']).DS;
ROOT_PATH=dirname(realpath(APP_PATH)).DS;
PHP_OS 返回系统常量\
public function pathinfo()
{
if (is_null($this->pathinfo)) {
if (isset($_GET[Config::get('var_pathinfo')])) {
// 判断URL里面是否有兼容模式参数
$_SERVER['PATH_INFO'] = $_GET[Config::get('var_pathinfo')];
unset($_GET[Config::get('var_pathinfo')]);
} elseif (IS_CLI) {
// CLI模式下 index.php module/controller/action/params/...
$_SERVER['PATH_INFO'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
}
// 分析PATHINFO信息
if (!isset($_SERVER['PATH_INFO'])) {
foreach (Config::get('pathinfo_fetch') as $type) {
if (!empty($_SERVER[$type])) {
$_SERVER['PATH_INFO'] = (0 === strpos($_SERVER[$type], $_SERVER['SCRIPT_NAME'])) ?
substr($_SERVER[$type], strlen($_SERVER['SCRIPT_NAME'])) : $_SERVER[$type];
break;
}
}
}
$this->pathinfo = empty($_SERVER['PATH_INFO']) ? '/' : ltrim($_SERVER['PATH_INFO'], '/');
}
return $this->pathinfo;
}
使用正则进行替换
public function path()
{
if (is_null($this->path)) {
$suffix = Config::get('url_html_suffix');
$pathinfo = $this->pathinfo();
if (false === $suffix) {
// 禁止伪静态访问
$this->path = $pathinfo;
} elseif ($suffix) {
// 去除正常的URL后缀
$this->path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo);
} else {
// 允许任何后缀访问
$this->path = preg_replace('/\.' . $this->ext() . '$/i', '', $pathinfo);
}
}
return $this->path;
}
echo $a;
print_r(error_get_last());
打印内容:
Array
(
[type] => 8
[message] => Undefined variable: a
[file] => C:\WWW\index.php
[line] => 2
) const CI_VERSION = '3.1.3'; //定义常量
//解析名称,如果首字母是小写 则替换成大写,
//如果UserName 则替换成user_name 这样的方式
// rpeg_replace_callback(正则,回调函数,匹配的内容) 的用法
function parseName($name, $type = 0, $ucfirst = true){
if ($type) {
$name = preg_replace_callback('/_([a-zA-Z])/', function ($match) {
var_dump($match);
return strtoupper($match[1]);
}, $name);
return $ucfirst ? ucfirst($name) : lcfirst($name);
} else {
return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
}
}
echo parseName("UserName"); //user_name
echo parseName("username",1); //Username
==============================================================
if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php'))
{
require_once(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
}
require_once(BASEPATH.'core/Common.php');
E_ERROR | E_PARSE | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $severity
set_error_handler('_error_handler');
set_exception_handler('_exception_handler');
register_shutdown_function('_shutdown_handler');
//定义php提醒错误 页面的布局
set_exception_handler('exception_handler');
function exception_handler($exception) {
echo "Uncaught exception: " , $exception->getMessage(), "\n";
}
throw new Exception('Uncaught Exception');
echo "Not Executed\n";
if ( ! empty($assign_to_config['subclass_prefix'])){
get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
}
$EXT =& load_class('Hooks', 'core');
/*
* ------------------------------------------------------
* Is there a "pre_system" hook?
* ------------------------------------------------------
*/
就是这样实现了数据的横切。其实这样的代码也是流式开发
$EXT->call_hook('pre_system');
$EXT->call_hook('post_controller_constructor');
...
$EXT->call_hook('post_controller');
require_once(BASEPATH.'core/compat/password.php');
举例:你看 也是从上到下的执行,也等于是流式的,怎么说是aop思想,横切?
//调用这个$this类中的$method 方法 ,传入$params
call_user_func_array(($this,$method),$params);
看看这个方法有什么用途?
class User{
public $name;
public $age;
public function __construct(){
}
public function __set($key,$val){
$this->$key=$val;
}
public function __get($key){
return $this->$key;
}
public function info($d,$message){
return "{$this->name}同学 {$this->age}岁"."-----{$d}:{$message}";
}
}
$user=new User();
$user->age=22;
$user->name='james';
$params=["say","大家好"];
//$params 这里传递的是一个数组,传递过去以后,与对应方法的参数一一对应
echo call_user_func_array(array($user,"info"),$params);
结果:james同学 22岁-----say:大家好
// Mark a start point so we can benchmark the controller
$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
$CI = new $class();
/*
* ------------------------------------------------------
* Is there a "post_controller_constructor" hook?
* ------------------------------------------------------
*/
$EXT->call_hook('post_controller_constructor');
/*
* ------------------------------------------------------
* Call the requested method
* ------------------------------------------------------
*/
call_user_func_array(array(&$CI, $method), $params);
// Mark a benchmark end point
$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
if (preg_match('#^'.$key.'$#', $uri, $matches))
{
// Are we using callbacks to process back-references?
if ( ! is_string($val) && is_callable($val))
{
// Remove the original string from the matches array.
array_shift($matches);
// Execute the callback using the values in matches as its parameters.
$val = call_user_func_array($val, $matches);
}
// Are we using the default routing method for back-references?
elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
{
$val = preg_replace('#^'.$key.'$#', $val, $uri);
}
$this->_set_request(explode('/', $val));
return;
}