PHP函数方法学习与总结
2018-03-10
PHP
Array
Immutable
map
1 | $f = function($x, ...$ys) {}; |
1 | xs.map(f) |
filter
1 | array_filter($xs, $f); |
1 | xs.filter(f) |
reduce
1 | array_reduce($xs, $f, $init); |
1 | xs.reduce(f, init) |
traverse
1 | $f = function($x, $key) {} |
1 | xs.forEach(f) |
slice
1 | array_slice($xs, $start, $len); |
1 | const end = start + len |
find
1 | array_search($x, $xs); // return false if not found |
1 | xs.indexOf(x) |
length
1 | count($xs); |
1 | xs.length |
has
1 | array_key_exists($key, $xs); |
1 | Reflect.has(xs, key) |
keys / values
1 | array_keys($xs); |
1 | Object.keys(xs) |
unique
1 | array_unique($xs); |
1 | [...new Set(xs)] |
fill
1 | array_fill($start, $len, $x); // fill by ref |
1 | const end = start + len |
concat / merge
1 | array_merge(...$xss); |
1 | [].concat(...xss) |
zip
1 | array_combine($xs, $ys); |
1 | xs.reduce((acc, x, i) => ({ ...acc, { [x]: ys[i] } }), {}) |
sum / product
1 | array_sum($xs); |
1 | xs.reduce((acc, x) => acc + x, 0) |
count
1 | array_count_values($xs); |
1 | xs.reduce((acc, x) => (Reflect.has(acc, x) ? acc[x]++ : (acc[x] = 1)) && acc, {}) |
Mutable
push / pop / unshift / shift
1 | $xs[] = $x; |
1 | // Mutable |
splice
1 | array_splice($xs, $start, $len, $ys); |
1 | xs.splice(start, len, ...ys) |
String
split
1 | explode($seperator, $str); |
1 | str.split(seperator) |
join
1 | implode($seperator, $xs); |
1 | xs.join(seperator) |
length
1 | strlen($str); |
1 | str.length |
substr
1 | substr($str, $start, $len); |
1 | const end = start + len |
Function
curry
1 | $curry = function($f, $len = -1) use(&$curry) { |
1 | const curry = (f, len = f.length) => (...args) => { |
compose
1 | $compose = function(...$funcs) { |
1 | const compose = (...funcs) => { |
Yii
WebApplication
Create
1 | Yii::createWebApplication($config) -> run(); |
retrieve
1 | Yii::app() // Singleton |
Components
retrieve
1 | Yii::app() -> $component; |
Controller
Route
default
1 | "https://hostname/index.php?r=$module/$controller/$action¶m=$param..." |
User-friendly
1 | // config.php |
Action
1 | class CommonController extends CController { |
1 | class EditAction extends CAction { |
Filter (Middleware)
1 | class CommonController extends CController { |
1 | class HelloFilter extends CFilter { |
1 | // Comparion |
Dependency Injection
1 | class Controller extends CController { |
Model
Db
multiple databases
1 | // config.php |
autoConnect
1 | $connection = Yii::app() -> db; |
connect
1 | $connection = new CDbConnection($dsn, $username, $password); |
Database Access Objects
command
1 | $command = $connection -> createCommand($sql); |
execute
1 | $command -> execute(); |
query
1 | $dataReader = $command -> query(); // forward-only stream |
1 | $data = $command -> queryAll(); |
transaction
1 | $transaction = $connection -> beginTransaction(); |
bind
1 | $sql = 'SELECT name FROM students WHERE id=:id'; |
Query Builder
command
1 | $command = $connection -> createCommand() |
Active Record
model
1 | class Student extends CActiveRecord { |
connection
1 | $connection = Student::model() -> dbConnection; |
create
1 | $student = new Student; |
retrieve
1 | $student = Student::model() -> find('id=:id', array(':id' => $id)); |
1 | $count = Student::model() -> count('name=:name', array(':name' => 'yanshi')); |
update
1 | $student = Student::model() -> findByPk(0); |
delete
1 | $student -> delete(); |
join
1 | $papers = Paper::model() -> findAll(array( |
区别bindParam,bindValue,bindValues
- bindParam 绑定变量的引用
- bindValue 绑定变量的值或者常量值
- bindValues 绑定多个值
AR调用save具体对应哪些操作
- $model 由 new 创建,执行 INSERT
- $model 由 find 返回,执行 UPDATE
1 | $model -> save(true, $attributes); // attributes need to be saved |
1 | $model -> updateAll($attributes, $condition, $param); |
empty,isset各种调用情况的返回值
- empty
- “”
- 0
- 0.0
- “0”
- NULL
- FALSE
- array()
- a variable declared, but without a value
- isset
- variable exsits and has value other than NULL
解释Yii的路由解析
showScript: false
入口文件添加 .htaccess 配置 Apache
1
2
3
4
5
6
7
8
9
10Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
设置header
1 | header("Access-Control-Allow-Origin: *"); |
redirect和forward的区别
1 | $this -> redirect($this -> createUrl("$controller/$action")); |
PHP怎么在长时间请求下不断往前端输出
1 | ob_get_level() || ob_start(); |
SESSION锁
1 | session_write_close(); |