zoukankan      html  css  js  c++  java
  • 014对象——对象 __isset __unset __sleep __wakeup

    <?php
    
    /**
     *
     */
    /*class lantian
    {
        public $name;
        public $age;
        private $money;
        public $c;
        function __construct($name, $arg = '', $money)
        {
            $this->name = $name;
            $this->age = $arg;
            $this->money = $money;
        }
    
        function __isset($var)
        {
            //设置可以在外面检验的变量
            $array = array('name', 'age');
            if (in_array($var, $array)) {
                // return isset($this->money);
                echo $var . "属性存在,他的值是:" . $this->$var;
            } elseif (in_array($var, array_keys(get_object_vars($this)))) {
                echo "变量不允许外部检测";
                return;
            } else {
                echo "属性不存在";
            }
        }
        //利用魔术:__unset()可以删除私有的属性
        function __unset($c)
        {
            if ($c=='age'){
                unset($this->$c);
                echo "删除属性{$c}成功";
            }else{
                echo "不允许删除属性{$c}";
            }
        }
        function get_money(){
            echo $this->money;
        }
    }
    
    $lisi = new lantian("李四", 22, 5500);
    
    //isset($lisi->name);
    
    //获取当前的页码
    //echo isset($_GET['page'])?$_GET['page']:1;
    $lisi->c=200;
    //echo $lisi->c;
    unset($lisi->age);
    //echo $lisi->get_money();*/
    
    
    /*$array1=array('php','html','mysql');
    $str=serialize($array1);//serialize() 产生一个可存储的值的表示。序列化数据
    $array2=unserialize($str);//unserialize() 从已存储的表示中创建 PHP 的值。反序列化
    print_r($array2);*/
    
    /*class db{
        private $host;
        private $user;
        private $pwd;
        private $dbname;
        function __construct($host,$user,$pwd,$dbname)
        {
            $this->host=$host;
            $this->user=$user;
            $this->pwd=$pwd;
            $this->dbname=$dbname;
            $this->db();
        }
        function db(){
            $mysqli=new mysqli($this->host,$this->user,$this->pwd,$this->dbname);
        }
        function select(){
            $sql="SELECT cate_id,cate_name FROM blog_category";
            $result=$this->mysqli->query($sql);
            while ($row=$result->fetch_assoc()){
                $rows[]=$row;
            }
            print_r($rows);
        }
        //反序列化魔术函数
        function __wakeup()
        {
            $this->db();
        }
    }
    session_start(); //打开session();
    $chanel=new db('localhost','root','','blog');
    //$chanel->select();
    //保存查找的内容,进行序列化,在别的页面进行调用:
    //$chanel_obj=serialize($chanel);
    //var_dump($chanel_obj);
    
    $_SESSION['chanel_obj']=serialize($chanel); //保存序列化之后的数据到session中。
    echo serialize($chanel);*/
    
    //
    class ren{
        private $name;
        private $age;
        function __construct($name,$age)
        {
            $this->name=$name;
            $this->age=$age;
        }
        function show(){
            echo "姓名是:{$this->name}  年龄是:{$this->age}";
        }
        //魔术方法:__sleep()指定序列化的属性
        function __sleep()
        {
            //return array('name','age');  //获取类的属性:
            print_r(array_keys(get_object_vars($this)));//get_object_vars()获取对象的变量属性:
        }
    }
    $zao=new ren('赵六',22);
    echo serialize($zao);
    

      

    <?php
    /**
     */
    session_start();
    include '14.php';
    $channel_obj=unserialize($_SESSION['channel_obj']);
    $channel_obj->select();
    

      

  • 相关阅读:
    apache多端口映射
    mark
    一些注册表值
    jsp URL中文处理的几种方式
    【引用】雨林木风Ghost XP SP3系统
    CentOS常用命令
    查看ie8临时文件夹
    卡塔兰数
    大数问题
    不会做的题目
  • 原文地址:https://www.cnblogs.com/yiweiyihang/p/8031072.html
Copyright © 2011-2022 走看看