1

PHP have what it calls "traits" which despite the name is not like traits in Rust, Scala or other languages.

In many other languages with support for traits, a trait create a is-a relation. So if class Foo implements the Bar trait then Foo is a Bar.

The same goes for "mixins" which also basically inheritance and creates a is-a relation.

PHP "traits" are different, they import the code directly into the class as if the code was moved into the class. This way no is-a (or has-a) relation is created.

Does any other language than PHP have what PHP calls "traits"?

Is there any other name for what PHP calls "traits"?

10
  • I don't know about other languages, but PHP traits are generally described as "compiler-assisted copy and paste".
    – bdsl
    Commented May 15, 2023 at 14:30
  • The word "mixin" does usually describe that copy-paste like relationship, without really interacting with the type system.
    – amon
    Commented May 15, 2023 at 15:21
  • See en.wikipedia.org/wiki/…
    – svidgen
    Commented May 15, 2023 at 20:31
  • @svidgen I've seen this article, but what PHP calls "traits" are quite different from what other languages call traits.
    – Fred
    Commented May 16, 2023 at 23:34
  • @user253751 The point is that in PHP the traits does not provide inheritance at all, but in other languages they often so, so when when you pass the type around, then it can get passed around to any place where the trait is asked for. My understanding is that PHP sucks the code into the class instead of inherit.
    – Fred
    Commented May 16, 2023 at 23:35

1 Answer 1

4

In PHP, "traits" are a way to reuse code in classes, providing a mechanism for horizontal code reuse. "Traits" allow you to define methods that can be used in multiple classes independently of the class hierarchy. While PHP has native support for "traits", not all programming languages have the exact concept of "traits".

Rust, Scala, Ruby, Swift, and Kotlin all have similar concepts as PHP "traits". Keep in mind, they're not the only languages with these concepts. If you're caught up in the specific syntax of "no is-a", you might have a hard time applying what you know about PHP "traits" and how their concepts are implemented in other languages.

Rust has a concept called "traits" that are similar to interfaces or protocols in other languages. Traits in Rust allow you to define a set of methods that can be implemented by multiple types. Types can then implement those traits, providing a way to reuse code across different data types.

Scala supports a feature called "traits," which are similar to PHP traits. Traits in Scala allow you to define reusable pieces of code that can be mixed into classes using multiple inheritance. These traits can define methods and fields that are then available to the classes that mix them in.

Ruby has a feature called "modules" that provide a similar level of code reuse. Modules allow you to define a set of methods that can be mixed into classes using the "include" keyword. Classes that include a module gain access to its methods and can override them if needed.

Swift has a feature called "protocols" that is similar to interfaces in other languages and shares some similarities with PHP traits. Protocols define a set of methods and properties that can be adopted by classes, structs, or enums. Types can then adopt multiple protocols, allowing for code reuse across different types.

Kotlin provides a feature called "interfaces" that serves a similar purpose as traits. Interfaces in Kotlin define a set of methods and properties that can be implemented by classes. A class can implement multiple interfaces, allowing code reuse across different classes.

EDIT: I forgot to note the most important answer for the final part of your question. Other languages would essentially call this "object oriented method declaration".

2
  • "object oriented method declaration" sounds very diffuse and I've never heard any language refer to it as such. I don't want inheritance. I don't want a is-a relationship because it misrepresents what it is. I am looking for horizontal code reuse in other languages (through declaration, not dependency injection)
    – Fred
    Commented May 16, 2023 at 23:42
  • 1
    The other examples I can think of and was able to research include Go, Crystal, and D. I could arguably put C# here too but it seems the is-a relationship model will fail you irrespective of the languages ability to provide what you want. I will say I haven’t found any language that matches exactly to traits. Go seems most compatible followed by D and then Crystal. Go supports composition through embedding, where a struct type can include another struct type as a field. This allows code reuse without creating an explicit is-a or has-a relationship. Crystal uses macros and D uses templates. Commented May 22, 2023 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.