php学习笔记(三)

2019-08-11  本文已影响0人  dev_winner

PHP类和对象

<?php
class Car {
    $name = '汽车';
    function getName() {
        return $this->name;
    }
}
$car = new Car();
echo $car->getName();
<?php
class Car {
    public static function getName() {
        return '汽车';
    }
​}
echo Car::getName(); //结果为“汽车”
<?php
class Car {
   function __construct() {
       print "构造函数被调用\n";
   }
}
$car = new Car(); //实例化的时候 会自动调用构造函数__construct,这里会输出一个字符串
<?php
class Car {
   function __construct() {
       print "父类构造函数被调用\n";
   }
}
class Truck extends Car {
   function __construct() {
       print "子类构造函数被调用\n";
       parent::__construct();
   }
}
$car = new Truck();
<?php
class Car {
   function __construct() {
       print "构造函数被调用 \n";
   }
   function __destruct() {
       print "析构函数被调用 \n";
   }
}
$car = new Car(); //实例化时会调用构造函数
echo '使用后,准备销毁car对象 \n';
unset($car); //销毁时会调用析构函数
<?php
class Car {
    private static $speed = 10;
    public static function getSpeed() {
        return self::$speed;
    }
}
echo Car::getSpeed();  //调用静态方法
$func = 'getSpeed';
$className = 'Car';
echo $className::$func();  //动态调用静态方法
<?php
class Car {
    private static $speed = 10;
    
    public static function getSpeed() {
        return self::$speed;
    }
    
    public static function speedUp() {
        return self::$speed+=10;
    }
}
class BigCar extends Car {
    public static function start() {
        parent::speedUp();
    }
}

BigCar::start();
echo BigCar::getSpeed();
<?php
class Car {
​    //默认为公有方法
    function turnLeft() {
    }
}
<?php
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(); //通过静态方法来获得一个实例
<?php
class Car {
    public $speed = 0; //汽车的起始速度是0

    public function speedUp() {
        $this->speed += 10;
        return $this->speed;
    }
}
//定义继承于Car的Truck类
class Truck extends Car {
    public function speedUp() {
        // 若此时想调用父类的同名方法,则需要这样调用:parent::同名方法名
        $this->speed = parent::speedUp() + 50;
    }
}
$car = new Truck();
$car->speedUp();
echo $car->speed; // 60

PHP类和对象之重载

<?php
class Car {
    private $ary = array();

    public function __set($key, $val) {
        echo '1、调用set方法' . '<br />';
        $this->ary[$key] = $val;
    }

    public function __get($key) {
        echo '2、调用get方法' . '<br />';
        if (isset($this->ary[$key])) {
            return $this->ary[$key];
        }
        return null;
    }

    public function __isset($key) {
        echo '3、调用isset方法' . '<br />';
        if (isset($this->ary[$key])) {
            return true;
        }
        return false;
    }

    public function __unset($key) {
        echo '4、调用unset方法' . '<br />';
        unset($this->ary[$key]);
    }
}
$car = new Car();
$car -> name = '汽车';  //name属性动态创建并赋值
echo $car -> name;
/* 输出结果:
1、调用set方法
2、调用get方法
汽车
*/
<?php
class Car {
    public $speed = 0;

    public function __call($name, $args) {
        if ($name == 'speedUp') {
            $this->speed += 10;
        }
    }
}
$car = new Car();
$car -> speedUp(); //调用不存在的方法会使用重载
echo $car -> speed; // 10
<?php
class Car {
}
$a = new Car();
$b = new Car();
if ($a == $b) echo '==';   //true
if ($a === $b) echo '==='; //false
<?php
class Car {
    public $name = 'car';
    
    public function __clone() {
        echo 'clone方法被调用<br />';
    }
}
$a = new Car();
$a->name = 'new car';
$b = clone $a;
var_dump($b);
<?php
class Car {
    public $name = 'car';
}
$a = new Car();
$str = serialize($a); //对象序列化成字符串
echo $str.'<br />';
$b = unserialize($str); //反序列化为对象
var_dump($b);
//O:3:"Car":1:{s:4:"name";s:3:"car";}
//object(Car)#2 (1) { ["name"]=> string(3) "car" }
上一篇下一篇

猜你喜欢

热点阅读