a mechanism for code reuse in single inheritance languages

php.net

 1 <?php
 2 class Base {
 3     public function sayHello() {
 4         echo ‘Hello‘;
 5     }
 6 }
 7
 8 trait SayWorld {
 9     public function sayHello() {
10         parent :: sayHello();
11         echo ‘World!‘;
12     }
13 }
14
15 class MyHelloWorld extends Base {
16     use SayWorld;
17 }
18
19 $o = new MyHelloWorld();
20 $o->sayHello();
21
22
23
24 trait HelloWorld {
25     public function sayHello() {
26         echo ‘Hello World‘;
27     }
28 }
29 class TheWorldIsNotEnough {
30     use HelloWorld;
31     public function sayHello() {
32         echo ‘Hello Universe!‘;
33     }
34 }
35
36 $ob = new TheWorldIsNotEnough();
37 $ob->sayHello();
38
39 trait Hello {
40     public function sayHello() {
41         echo ‘Hello‘;
42     }
43 }
44
45 trait World {
46     public function sayWorld() {
47         echo ‘World‘;
48     }
49 }
50
51 class MyHelloWorld_c {
52     use Hello, World;
53     public function sayExclamationMark() {
54         echo ‘!‘;
55     }
56 }
57
58 $oc = new MyHelloWorld_c();
59 $oc->sayHello();
60 $oc->sayWorld();
61 $oc->sayExclamationMark();

Precedence
An inherited member from a base class is overridden by a member inserted  by a Trait. The precedence order is that members from the current class override Trait methods, which in turn override inherited methods.

The precedence order is that methods from the current class override Trait methods, which in turn override methods from the base class.

A Trait is similar to a class, but only intended to group functionality in a  fine-grained and consistent way. It is not possible to instantiate a Trait on  its own. It is an addition to traditional inheritance and enables horizontal composition of behavior; that is, the application of class members without requiring inheritance.

时间: 2024-10-09 16:05:54

a mechanism for code reuse in single inheritance languages的相关文章

[JS Compose] 1. Refactor imperative code to a single composed expression using Box

After understanding how Box is, then we are going to see how to use Box to refacotr code, to un-nested expression. For example, we have code: const moneyToFloat = str => { const cost = str.replace(/\$/g, ''); return parseFloat(cost); } const percentT

Java Interview Reference Guide--reference

Part 1 http://techmytalk.com/2014/01/24/java-interview-reference-guide-part-1/ Posted on January 24, 2014 by Nitin Kumar JAVA Object Oriented Concepts Java in based on Object Oriented concepts, which permits higher level of abstraction to solve any p

Classical Inheritance in JavaScript

JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. This can be puzzling to programmers trained in conventional object-oriented languages like C++ and Java. JavaScript's

Inheritance versus composition

One of the fundamental activities of an object-oriented design is establishing relationships between classes. Two fundamental ways to relate classes are inheritance and composition. Although the compiler and Java virtual machine (JVM) will do a lot o

JavaScript- The Good Parts Chapter 5 Inheritance

Divides one thing entire to many objects;Like perspectives, which rightly gazed uponShow nothing but confusion. . .—William Shakespeare, The Tragedy of King Richard the Second Inheritance is an important topic in most programming languages. In the cl

Natively Compiled Code: A Comeback?

RAD Studio and Natively Compiled Code In today's development landscape, natively compiled code is making a significant comeback. RAD Studio has always been focused on it. In today's development landscape, natively compiled code is making a significan

Building Maintainable Software-java篇之Write Short Units of Code

Building Maintainable Software-java篇之Write Short Units of Code Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -Martin Fowler Guideline: ? Limit the length of code units to 15 lines of c

如何在 PhpStorm 使用 Code Generation?

實務上開發專案時,有一些程式碼會不斷的出現,這時可靠 PhpStorm 的 Code Generation 幫我們產生這些 code snippet,除此之外,我們也可以將自己的 code snippet 加入 Live Template,可加快發開速度,並減少 typo. Version PHP 7.0.8Laravel 5.2.41PhpStorm 2016.2 Namespace Laravel 5 的 app 目錄下都遵循 PSR-4,也就是每個在 app 目錄下的 class 都要有

Top 40 Static Code Analysis Tools

https://www.softwaretestinghelp.com/tools/top-40-static-code-analysis-tools/ In this article, I have summarised some of the top static code analysis tools. Can we ever imagine sitting back and manually reading each line of codes to find flaws? To eas