PHP语法笔记

$array1 = range(1000, 2000); //生成值为 1000~2000 长度为 1001 的数组
shuffle($array1); //打乱数组顺序
$array_merged = array_merge($array1, $array2); //合并数组

number_format($today_weixin,2);//格式化数据,保留小数

isset($array1[$i]); //效率高
array_key_exists($i, $array1); //效率低
time php index.php //计算运行脚本需要的时间
//:q!  不保存修改退出
//:wq 保存修改退出

class Test {
    private $var = "123";
    private $name = "456";
    public function __get ($varname) { //使用魔法函数 效率低
        return $this->$varname;
    }

    public $age = "111";
}
$test = new Test();
var_dump($test->age);

 

闭包

function test($num1, $num2) {
    $args = func_get_args();
    echo $args[2]($num1, $num2);
}

test(12, 13, function($a, $b) {
    return "12+13 = " . ($a + $b);
});

 

//
$stack = new SplStack();
$stack->push("data1<br/>");
$stack->push("data2<br/>");

echo $stack->pop();
echo $stack->pop();

//队列
$queue = new SplQueue();
$queue->enqueue("data1<br/>");
$queue->enqueue("data2<br/>");

echo $queue->dequeue();
echo $queue->dequeue();

//
$heap = new SplMinHeap();
$heap->insert("data1<br/>");
$heap->insert("data2<br/>");

echo $heap->extract();
echo $heap->extract();

//固定尺寸数组
$array = new SplFixedArray(10);
$array[0] = 123;
$array[8] = 345;

var_dump($array);

 部分魔术方法

    //__get __set 在无该属性的时候才调用
    public function __get($name) {
        echo "__get:" . $name;
    }

    public function __set($name, $value) {
        echo "__set:" . $name . "," . $value;
    }
    //__call 在无该方法的时候才调用
    public function __call($name, $arguments) {
        echo "__call:" . $name . "(" . implode(", ", $arguments) . ")";
        return "无该方法";
    }
    //__callStatic 无该静态方法的时候才调用
    public static function __callStatic($name, $arguments) {

    }
    //调用对象实例的时候调用
    public function __toString() {
        return "__toString";
    }
    //实例当函数使用时会调用  $xxx("参数")
    public function __invoke($obj) {
        echo "__invoke:" . $obj;
    }

 单例

class Index {

    //单例
    protected static $index;

    public $name; //例子

    //把构造方法设置为 private 该对象就不能 new 了
    private function __construct() {

    }

    static function getInstance() {
        if(self::$index) {
            return self::$index;
        }
        self::$index = new self();
        return self::$index;
    }

}

 注册树

class Index {
    //注册树
    protected static $objects;
    static function set($alias, $object) {
        self::$objects[$alias] = $object;
    }
    static function get($alias) {
        return self::$objects[$alias];
    }
    static function _unset($alias) {
        unset(self::$objects[$alias]);
    }
}

 观察者模式

abstract class EventGenerator {
    private $observers = array();
    function addObserver(Observer $observer) {
        $this->observers[] = $observer;
    }
    function notify() {
        foreach ($this->observers as $observer) {
            $observer->update();
        }
    }
}

interface Observer {
    function update($event_info = null);
}

class Event extends EventGenerator {
    function trigger() {
        $this->notify();
    }
}

//观察者
class Observer1 implements Observer {
    function update($event_info = null) {
        echo "逻辑1";
    }
}


$event = new Event();
$event->addObserver(new Observer1());
$event->trigger();

 原型模式(new一次的不需要再new了)

$event2 = clone $event;

 迭代器

class AllUser implements Iterator {
    protected $ids;
    protected $index;
    public function __construct() {
        $this->ids = [1, 2, 3, 4, 5];
    }

    function current() {
        $id = $this->ids[$this->index];
        return $id;
    }
    function next() {
        $this->index ++;
    }
    //验证是否还有下一个
    function valid() {
        return $this->index < count($this->ids);
    }
    //置位迭代器
    function rewind() {
        $this->index = 0;
    }
    //位置
    function key() {
        return $this->index;
    }
}

foreach (new AllUser() as $user) {
    var_dump($user);
}

 

posted @ 2018-08-26 17:22  刘冠  阅读(144)  评论(0编辑  收藏  举报