• PHP:类(class)和接口( interface )


    PHP 类和接口

    一、类和接口 

    <?php 
    
    // php_version = 7.4.1
    
    interface information 
    {
        // __say(): print class name
        public function __say():void;
        
        // __msg(): print attributes and method of class
        public function __msg():void;
    }
        
    class back implements information
    {
        private $back_name;
        private $back_age;
            
        // interface function
        public function __say():void
        {
            echo PHP_EOL;
            echo "this_class_name = " . __CLASS__ .PHP_EOL;
        }
        
        // interface function
        public function __msg():void
        {
            echo PHP_EOL;
            echo "this_class_name = " . __METHOD__ .PHP_EOL;
            echo __CLASS__ . ".FUNCTIONS = " . "6, say, msg, display, set, get_name, get_age ." .PHP_EOL;
    
            echo "this_class_name = " . __METHOD__ .PHP_EOL;
            echo __CLASS__ . ".ATTRIBUTES = " . "2, name, age." .PHP_EOL;
        }
        
        public function set($name, $age)
        {
            $this -> back_name = $name;
            $this -> back_age = $age;
        }
        
        public function get_name()
        {
            return $this -> back_name;
        }
        public function get_age()
        {
            return $this -> back_age;
        }
        
        public function display()
        {
            echo PHP_EOL;
            echo "name = " . $this -> back_name ;
            echo " ,   age = " . $this -> back_age .PHP_EOL;
        }
        
        public function say()
        {
            $this -> __say();
        }
        
        public function msg()
        {
            $this -> __msg();
        }
        
    }
        
    
    // instances of back
    $tmp = new back();
    $tmp -> say();
    $tmp -> msg();
    
    // test set() display() 
    $tmp -> set("laohu", 3);
    $tmp -> display();
    
    // test get_name() and get_age()
    $temp_name = $tmp -> get_name();
    echo "temp_name = " . $temp_name .PHP_EOL;
    $temp_age = $tmp -> get_age();
    echo "temp_age = " . $temp_age .PHP_EOL;
        
    
    ?> 

    输出结果:

    // 输出结果
    
    
    this_class_name = back
    
    this_class_name = back::__msg
    back.FUNCTIONS = 6, __say, __msg, display, set, get_name, get_age .
    this_class_name = back::__msg
    back.ATTRIBUTES = 2, name, age.
    
    name = laohu ,   age = 3
    temp_name = laohu
    temp_age = 3
     

    二、trait :功能是函数代码重用

    <?php 
    
    // php_version = 7.4.1
    
    // trait 功能“函数”复用
    trait hello
    {
        public function say_hello()
        {
            echo PHP_EOL;
            echo "hello ,  ";
        }
    }
    
    trait world
    {
        public function say_world()
        {
            echo "world !" .PHP_EOL;
        }
    }
    
    // 只需引用 trait 定义的代码块,“实例”可以直接调用它的函数
    class laohu
    {
        // 引用trait定义的代码块
        use hello, world;    
    }
    
    
    // 建立 laohu对象
    $tmp = new laohu();
    
    // 调用“trait”定义的代码块的方法
    $tmp -> say_hello();
    $tmp -> say_world();
    
    ?> 

    输出结果:

    // 输出结果:
    
    hello ,  world !

    三、实例的序列化

    <?php 
    
    // php_version = 7.4.1
    
    
    // trait 功能“函数”复用
    trait hello
    {
        public function say_hello()
        {
            echo PHP_EOL;
            echo "hello ,  ";
        }
    }
    
    trait world
    {
        public function say_world()
        {
            echo "world !" .PHP_EOL;
        }
    }
    
    
    // 只需引用 trait 定义的代码块,“实例”可以直接调用trait定义的代码块的函数
    class laohu
    {
        protected $name = "your name";
        protected $age = "3";
        
        // 引用trait定义的代码块
        use hello, world;
        
        public function msg()
        {
            echo PHP_EOL;
            echo "name = " .$this -> name;
            echo " ,   age = " .$this -> age .PHP_EOL;
        }
    }
    
    
    // 建立 laohu对象
    $tmp = new laohu();
    
    // 序列化“实例 $tmp”
    $sa = serialize($tmp);
    echo $sa . PHP_EOL;
    
    // 保存序列化“$sa ”内容
    file_put_contents('save', $sa);
    
    
    // 获取序列化“$sa ”的内容
    $unseq = file_get_contents('save');
    
    // 反序列化“实例 $tmp”
    $unsa = unserialize($unseq);
    $unsa -> say_hello();
    $unsa -> msg();
    
    ?> 

    输出结果:

    // 输出结果
    
    O:5:"laohu":2:{s:7:"*name";s:9:"your name";s:6:"*age";s:1:"3";}
    
    hello ,  
    name = your name ,   age = 3

    四、协变和逆变

           1、协变:子类的方法的返回值类型更具体,较于父类的返回值类型;

      2、逆变:子类的函数可接受的参数类型更广泛,较于父类的可接受的参数类型;

     

    五、

    本文由lnlidawei(https://www.cnblogs.com/lnlidawei)原创或整理,转载请注明出处。
  • 相关阅读:
    刷新验证码
    网页的超链接传递中文参数乱码问题
    Button获取Repeater一行的两个值
    <asp:FileUpload>上传一张图片并且重命名
    <asp:DropDownList数据库读取
    <asp:DropDownList>用法
    <%#Eval(" ")%>用法总结
    DropDownList1 .cs指定初始值
    redolog(未完工)
    cap理论
  • 原文地址:https://www.cnblogs.com/lnlidawei/p/15659657.html
Copyright © 2020-2023  润新知