Anonymous fields for PHP trait and go struct

Source: Internet
Author: User
first of all, the concept of trait in PHP is the concept of struct anonymous field in the Go language. is the desire to incorporate one structure into another, adding functionality to another structure. Rather than through tree-shaped inheritance PHP Trait Demo
<?php

    class Base {public
        function SayHello () {
            echo ' Hello ';
        }
    }

    Trait SayHello {public
        function SayHello () {
            Parent::sayhello ();
            echo ' World! ';
        }

        Public Function Sayhi () {
            echo ' sayhi ';
        }
    }

    Class Myhelloworld extends Base {use
        sayhello;
    }


    $helloWorld = new Myhelloworld ();

    $helloWorld->sayhello ();
    $helloWorld->sayhi ();
# #输出结果
Hello World!sayhi
# #说明用trait定义的结构可以增强其他结构的功能
Go language struct anonymous field
package main import "FMT" type Color struct {red string yellow string Blue string} Type iphone struct {Color//anonymous field, in order to enhance the iphone this data structure of size float32 memory string} func main () {Iphone: = iphone{color{"Red", "yellow", "Blue"}, 4.8, "256G"} fmt. Println (iphone.red, Iphone.yellow, Iphone.blue, Iphone.size, iphone.memory)} 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.