PHP知识

来源:互联网 发布:java ioc简书 编辑:程序博客网 时间:2024/05/18 01:21
Yaf_Dispatcher::setView — Set a custom view engine

public Yaf_Dispatcher Yaf_Dispatcher::setView ( Yaf_View_Interface $view )

This method proviods a solution for that if you want use a custom view engine instead of Yaf_View_Simple


Network Design & Optimisation (NDO)

在上面你会看到一个Smarty_Adapter的类,没错,这就是我上面说的那个适配器,你需要写这么一个适配器,把你需要view做的功能实现在里面,yaf通过他们来操作smarty的特性。下面给你一个Smarty_Adapter的demo(按名称应该知道他应该是Smarty目录下的一个叫Adapter.php的文件)。


在子类的对象上可以直接访问父类中的方法和属性

php是单继承

var 是PHP4的时候用的,它和现在的Public作用一样,现在就用Public了,PHP4的时候没有Public,Private,Protected,都只有一个var

基本都是使用public来代替var,PHP5中,var等同于public


访问控制通过关键字public,protected和private来实现。被定义为公有的类成员可以在任何地方被访问。被定义为受保护的类成员则可以被其自身以及其子类和父类访问。被定义为私有的类成员则只能被其定义所在的类访问。

类属性必须定义为公有、受保护、私有之一。为兼容PHP5以前的版本,如果采用 var 定义,则被视为公有。

类属性必须定义访问控制,而没有默认的,除非是var


但是类中的方法可以被定义为公有、私有或受保护。如果没有设置这些关键字,则该方法默认为公有。


如果构造函数定义成了私有方法,则不允许直接实例化对象了,这时候一般通过静态方法进行实例化,在设计模式中会经常使用这样的方法来控制对象的创建,比如单例模式只允许有一个全局唯一的对象。

class Car {
    private function __construct() {
        echo 'object create';
    }

    private static $_object = null;
    public static function getInstance() {
        if (empty(self::$_object)) {
            self::$_object = new Car(); //内部方法可以调用私有方法,因此这里可以创建对象
        }
        return self::$_object;
    }
}
//$car = new Car(); //这里不允许直接实例化对象
$car = Car::getInstance(); //通过静态方法来获得一个实例



保护方法能否被实例化的对象调用?





对象复制,在一些特殊情况下,可以通过关键字clone来复制一个对象,这时__clone方法会被调用,通过这个魔术方法来设置属性的值。

class Car {
    public $name = 'car';
    
    public function __clone() {
        $obj = new Car();
        $obj->name = $this->name;
    }
}
$a = new Car();
$a->name = 'new car';
$b = clone $a;
var_dump($b);



对象序列化,可以通过serialize方法将对象序列化为字符串,用于存储或者传递数据,然后在需要的时候通过unserialize将字符串反序列化成对象进行使用。

class Car {
    public $name = 'car';
}
$a = new Car();
$str = serialize($a); //对象序列化成字符串
echo $str.'<br>';
$b = unserialize($str); //反序列化为对象
var_dump($b);


    fork is expensive. Memory is copied from the parent to the child, all descriptors are duplicated in the child, and so on. Current implementations use a technique called copy-on-write, which avoids a copy of the parent's data space to the child until the child needs its own copy. But, regardless of this optimization, fork is expensive.

    IPC is required to pass information between the parent and child after the fork. Passing information from the parent to the child before the fork is easy, since the child starts with a copy of the parent's data space and with a copy of all the parent's descriptors. But, returning information from the child to the parent takes more work.

Threads help with both problems. Threads are sometimes called lightweight processes since a thread is "lighter weight" than a process. That is, thread creation can be 10–100 times faster than process creation.

All threads within a process share the same global memory. This makes the sharing of information easy between the threads, but along with this simplicity comes the problem of synchronization.
0 0
原创粉丝点击