|
|
克隆对象
- <?php
- class demo{
- public $name;
- public $age;
- public function __construct($name, $age){
- $this -> name = $name;
- $this -> age = $age;
- }
- public function say(){
- echo "say ".$this-> name;
- }
- //__clone() 魔术方法 是在克隆对象时被自动调用的
- //作用:可以对新对象的成员属性进行赋值
- public function __clone(){
- $this -> name = "lisi";
- $this -> age = 20;
- }
- }
- $demo = new demo("zhangsan", 18);
- $demo -> say();
- $demo1 = clone $demo;
- echo "<hr />";
- $demo1 -> say();
- var_dump($demo1);
复制代码
类中通用的方法 __toString( )
- <?php
- class demo{
- public $name;
- public function __construct($name){
- $this -> name = $name;
- }
- //魔术方法__toString() 是直接echo 或print 对象时被自动调用
- //作用:可以直接返回字符串或用于调用流程处理
- public function __toString(){
- $this -> d();
- $this -> e();
- return '';
- }
- private function d(){
- echo "d....";
- }
- private function e(){
- echo "e...";
- }
- }
- $demo = new demo("zhangsan");
- echo $demo;
复制代码
__call( ) 方法的应用
- <?php
- class db{
- private $sql = array("table" => '',
- "field" => '*',
- "where" => '',
- "order" => '',
- "limit" => '');
- //魔术方法 __call() 是在调用 一个不存在的方法时被自动调用
- //第一个参数:调用的方法名
- //第二个参数:调用方法时传的参数列表(数组)
- /*
- public function __call($methodName, $args){
- echo "你所调用的方法{$methodName}(),参数:";
- print_r($args);
- echo "不存在!";
- }
- */
- public function __call($methodName, $args){
- //判断调用的方法名是否是成员属性数组的下标
- if(array_key_exists($methodName,$this-> sql)){
- //如果是就进行赋值操作
- $this -> sql[$methodName] = $args[0];
- }else{
- //如果不是就给出提示信息
- die("你所调用的方法{$methodName}()不存在!");
- }
- //返回本对象,为了实现连惯操作
- return $this;
- }
- public function select(){
- if($this -> sql['where']){
- $where = "WHERE {$this -> sql['where']}";
- }
- if($this -> sql['order']){
- $order = "ORDER {$this -> sql['order']}";
- }
- if($this -> sql['limit']){
- $limit = "LIMIT {$this -> sql['limit']}";
- }
- $sql = "SELECT {$this->sql['field']} FROM {$this -> sql['table']} {$where} {$order} {$limit}";
- echo $sql;
- }
- }
- $db = new db();
- $db -> table("user")->field("id,username,pwd") -> where("id <100") -> select();
- var_dump($db);
复制代码
自动加载类
- <?php
- //__autoload() 是在实例化对象时,如果类不存在就会被自动调用
- //参数:实例化的类名
- //作用:可以用于自动引入类文件
- function __autoload($className){
- //注意:类文件名要有规律
- // 类文件名要与类名统一的部分
- // 类文件的路径要有规律
- $file = $className.".class.php";
- $path = "./class/".$file;
- if(file_exists($path)){
- include($path);
- }else{
- die("你调用使用的{$className}.class.php 文件不存在");
- }
- }
- $demo = new demo();
- var_dump($demo);
- $demo1 = new demo1();
复制代码 ./class/demo.class.php
对象串行化
- <?php
- class demo{
-
- public $name;
- public $age;
- public $sex;
- public function __construct($name, $age, $sex){
- $this -> name = $name;
- $this -> age = $age;
- $this -> sex = $sex;
- }
- //魔术方法__sleep() 是在串行化对象时被自动调用
- public function __sleep(){
- //返回一个数组,数组的值就是要串行化的成员属性名
- return array("name","sex","age");
- }
- //魔术方法 __wakeup() 是在反串行化对象被自动调用
- public function __wakeup(){
- //可以把发生改变的成员属性进行重新的赋值操作
- $this->age = $this -> age + 1;
- }
- }
- /*
- $demo = new demo("lisi",20,"nan");
- $str = serialize($demo); //串行化 可以串行化数组也可以串行化对象,串行化对象时只是串行化了成员属性
- $handle = fopen("./data.txt","w+");
- fwrite($handle,$str);
- fclose($handle);
- var_Dump($str);
- */
复制代码- <?php
- $arr = array("name"=>"zhangsan","age"=>18,"sex"=>"nv");
- $str = serialize($arr);
- var_dump($str);
- $arr1 = unserialize($str);
- var_dump($arr1);
复制代码- <?php
- include("./demo.class.php");
- $str = file_get_contents("./data.txt");
- $d = unserialize($str); //可以把串行化的结果进行反串行化操作
- var_dump($d->age);
复制代码
|
|