Skip to content

Commit

Permalink
后期静态绑定
Browse files Browse the repository at this point in the history
  • Loading branch information
OMGZui committed Oct 17, 2018
1 parent 1330a23 commit 044a1ae
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
65 changes: 65 additions & 0 deletions php.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,71 @@ $greet('PHP');
### 1、类与对象
类的基本概念:`class`,`new`,`extends`,`::class`
属性和方法:`public`,`protected`,`private`
类常量:`self`,`parent`,`static`
自动加载:`spl_autoload_register()`
构造函数和析构函数:`__construct()`,`__destruct()`
范围解析操作符:`::`
抽象类:`abstract`
对象接口:`interface`,`implements`
Trait:`trait`
重载:`__set()`,`__get()`,`__isset()`,`__unset()`,`__call()`,`__callStatic()`
魔术方法:`__toString()`,`__invoke()`
Final:`final`
对象复制:`clone`
后期静态调用:
```php
<?php
class A {
public static function foo() {
static::who(); // 后期静态调用
}
public static function who() {
echo __CLASS__."\n";
}
}
class B extends A {
public static function test() {
A::foo(); //非转发
parent::foo(); //转发
self::foo(); //转发
}
public static function who() {
echo __CLASS__."\n";
}
}
class C extends B {
public static function who() {
echo __CLASS__."\n";
}
}
C::test();
输出:
A
C
C
```
### 2、命名空间
### 3、异常处理
Expand Down
50 changes: 49 additions & 1 deletion php/demo/IteratorDemo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,52 @@
* User: 小粽子
* Date: 2018/10/17
* Time: 17:27
*/
*/

namespace PHP\Demo;

require __DIR__ . '/../bootstrap.php';

class IteratorDemo implements \Iterator
{
private $var = [];

public function __construct($arr)
{
if (is_array($arr)) {
$this->var = $arr;
}
}

public function rewind()
{
reset($this->var);
}

public function current()
{
return current($this->var);
}

public function key()
{
return key($this->var);
}

public function next()
{
return next($this->var);
}

public function valid()
{
return $this->current() !== false;
}
}

$it = new IteratorDemo([1, 2, 3]);
dump($it);

foreach ($it as $k => $item) {
dump("$k:$item");
}

0 comments on commit 044a1ae

Please sign in to comment.