最近花了一个星期翻了一遍PHP手册, 算是一次拾遗~
运算符
and/or/xor运算符
PHP还可以用and/or/xor的逻辑运算符~
var_dump(false and true);// 与 false
var_dump(false or true); // 或 true
var_dump(true xor false);// 异或 true
会不会因为可移植性的问题,而不推荐使用....?
<=>运算符
$smaller = 1 <=> 2;
$equal = 1 <=> 1;
$lager = 2 <=> 1;
var_dump($smaller);
var_dump($equal);
var_dump($lager);
//-1 小于 0等于 1 大于
那么小于等于,大于等于怎样表示?
$equalOrSamller = (1 <=> 2) != 1;
$equalOrLager = (1 <=> 2) != -1;
//小于等于 == 不大于 , 大于等于 == 不小于
不过这个不知道怎样用应该什么时候使用捏
??运算符
PHP7新出的??运算符,再也不用使用一个小函数来设置默认值了
function defaultValue(&$var,$default){
if(!isset($var))
return $default;
return $var;
}
defaultValue($temp,'a');
现在可以和JS类似的写法~
$var = $args ?? 'default value';
file_get_content也可以发送Post请求
看了手册才发现,其实file_get_content一样可以post,设置cookies,感觉比Curl直观多了~当然参数不是很多,相比于Curl
$opts = array(
'http' =>array(
'proxy' => null,
'method' => null,
'header' => null,
'timeout' => null,
'content' => null,
'user_agent' => null,
'max_redirects' => null,
'ignore_errors' => null,
'request_fulluri' => null,
'follow_location' => null,
'protocol_version' => null
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/submit.php', false, $context);
//或者
$stream = fopen('http://example.com/submit.php', 'r', false, $context);
var_dump(stream_get_meta_data($stream));//响应报文的信息
var_dump(stream_get_contents($stream));
fclose($stream);
一样可以创建之后修改options
stream_context_set_option($context,$opts);
来个小小性能对比,循环100次get http://www.baidu.com
不过把循环搬到发送那一步,就是变成这样,curl的优势体现出来了~~
看了手册才发现原来file_get_content一样是可以post请求的,所以还是看官网手册自己写Demo实测比较好~~
详细的使用方式去看手册吧file_get_content/fopen其实PHP所封装的协议应该都可以用的,不仅仅是http(s),反正去看手册就是啦
生成器
它的workflow我的理解方式是这样的~
function genaretor(){
$receive1 = yield 'send out 1';
yield $receive1;
$receive2 = yield 'send out 2';
// var_dump($this);
// $this->next();这样不行~就是说只能外部控制咯~
//Fatal error: Uncaught Error: Using $this when not in object context
yield $receive2;//入口和出口绑定一起了~可能有时候只想出,不想进~
}
$gen = genaretor();
var_dump($gen);//class Generator#4 (0) {}
var_dump($gen);//Generator::__set_state(array()) , 记得魔术方法里面有__set_state()
$gen->rewind();
var_dump($gen->current());//send out 1
// $gen->next();
// $gen->rewind();
//Fatal error: Uncaught Exception: Cannot rewind a generator that was already run
var_dump($gen->send('send in 1'));//send in 1
var_dump($gen->current());//send in 1
var_dump($gen->next());//null next不会返回数据
var_dump($gen->current());//send out 2
var_dump($gen->send('send in 2'));
$gen->next();
var_dump($gen->current());//send out 2
这家伙背后是协程的实现....请直接移步鸟哥的翻译优化版
不过我现在需要处理的数据结构也不是很复杂,,,,希望大牛举几个例子呗~~
ArrayAccess接口
这个可以理解为[]的重载为对象的数据获取方式增添的另一种方式吧
class arrayLikeObj implements ArrayAccess {
private $container = array();
public function __construct($arr) {
$this->container = $arr;
}
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->container[] = $value;
} else {
$this->container[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->container[$offset]);
}
public function offsetUnset($offset) {
unset($this->container[$offset]);
}
public function offsetGet($offset) {
return isset($this->container[$offset]) ? $this->container[$offset] : null;
}
}
$obj = new arrayLikeObj(array(
"one" => 1,
"two" => 2,
"three" => 3,
));
var_dump($obj[1]);
反正就是数组运算符[]
的重载咯~只是和其他语言的实现的方式不懂而已,不知道怎样实现其他运算符的重载比如<
==
>
还有命名空间
这个只是我自己用得比较少,还是贴在这啦~
只要搞懂手册这个寻址规则就没问题了
发现的拓展
发现一个v8js
(V8 Javascript Engine Integration)的扩展,不知道为什么惊喜了一晚上,不过到另一天又想不到有什么用途,爬虫时候实现js跳转?....
还有FANN (Fast Artificial Neural Network),之前还看到PHP-ML, 感觉PHP还是挺时髦的啊~
对对, 还有关于PHP的引用
在取消引用
那里的user notes讲得很好~
/* Imagine this is memory map
______________________________
|pointer | value | variable |
--------------------------------
| 1 | NULL | --- |
| 2 | NULL | --- |
| 3 | NULL | --- |
| 4 | NULL | --- |
| 5 | NULL | --- |
------------------------------------
Create some variables */
$a=10;
$b=20;
$c=array ('one'=>array (1, 2, 3));
/* Look at memory
_______________________________
|pointer | value | variable's |
-----------------------------------
| 1 | 10 | $a |
| 2 | 20 | $b |
| 3 | 1 | $c['one'][0] |
| 4 | 2 | $c['one'][1] |
| 5 | 3 | $c['one'][2] |
------------------------------------
do */
$a=&$c['one'][2];
/* Look at memory
_______________________________
|pointer | value | variable's |
-----------------------------------
| 1 | NULL | --- | //value of $a is destroyed and pointer is free
| 2 | 20 | $b |
| 3 | 1 | $c['one'][0] |
| 4 | 2 | $c['one'][1] |
| 5 | 3 | $c['one'][2] ,$a | // $a is now here
------------------------------------
do */
$b=&$a; // or $b=&$c['one'][2]; result is same as both "$c['one'][2]" and "$a" is at same pointer.
/* Look at memory
_________________________________
|pointer | value | variable's |
--------------------------------------
| 1 | NULL | --- |
| 2 | NULL | --- | //value of $b is destroyed and pointer is free
| 3 | 1 | $c['one'][0] |
| 4 | 2 | $c['one'][1] |
| 5 | 3 |$c['one'][2] ,$a , $b | // $b is now here
---------------------------------------
next do */
unset($c['one'][2]);
/* Look at memory
_________________________________
|pointer | value | variable's |
--------------------------------------
| 1 | NULL | --- |
| 2 | NULL | --- |
| 3 | 1 | $c['one'][0] |
| 4 | 2 | $c['one'][1] |
| 5 | 3 | $a , $b | // $c['one'][2] is destroyed not in memory, not in array
---------------------------------------
next do */
$c['one'][2]=500; //now it is in array
/* Look at memory
_________________________________
|pointer | value | variable's |
--------------------------------------
| 1 | 500 | $c['one'][2] | //created it lands on any(next) free pointer in memory
| 2 | NULL | --- |
| 3 | 1 | $c['one'][0] |
| 4 | 2 | $c['one'][1] |
| 5 | 3 | $a , $b | //this pointer is in use
---------------------------------------
lets tray to return $c['one'][2] at old pointer an remove reference $a,$b. */
$c['one'][2]=&$a;
unset($a);
unset($b);
/* look at memory
_________________________________
|pointer | value | variable's |
--------------------------------------
| 1 | NULL | --- |
| 2 | NULL | --- |
| 3 | 1 | $c['one'][0] |
| 4 | 2 | $c['one'][1] |
| 5 | 3 | $c['one'][2] | //$c['one'][2] is returned, $a,$b is destroyed
--------------------------------------- ?>
I hope this helps.
有一些看了但是过一天就没印象了,看手册真的很困~~
想起来一个,declare 也是很少用的,不过背后也是可以实现超级厉害的东西,反正我就看不懂咯,希望有大神讲解一下下~