标签:
1 class Car {
2 function __construct() {
3 print "父类构造函数被调用\n";
4 }
5 }
6 class Truck extends Car {
7 function __construct() {
8 print "子类构造函数被调用\n";
9 parent::__construct();
10 }
11 }
12 $car = new Truck();
1 class Car {
2 private function __construct() {
3 echo ‘object create‘;
4 }
5
6 private static $_object = null;
7 public static function getInstance() {
8 if (empty(self::$_object)) {
9 self::$_object = new Car(); //内部方法可以调用私有方法,因此这里可以创建对象
10 }
11 return self::$_object;
12 }
13 }
14 //$car = new Car(); //这里不允许直接实例化对象
15 $car = Car::getInstance(); //通过静态方法来获得一个实例
1 class Car {
2 public $name = ‘car‘;
3 }
4 $a = new Car();
5 $str = serialize($a); //对象序列化成字符串
6 echo $str.‘<br>‘;
7 $b = unserialize($str); //反序列化为对象
8 var_dump($b);
PHP允许我们在双引号串中直接包含字串变量
。
而单引号串中的内容总被认为是普通字符。
比如:
$str=‘hello‘; echo "str is $str"; //运行结果: str is hello echo ‘str is $str‘; //运行结果: str is $str
标签:
原文地址:http://www.cnblogs.com/vanone/p/5373627.html