Skip to main content

Command Palette

Search for a command to run...

Object Oriented Programming(OOP) Series: Traits

Traits in simple terms with examples

Updated
2 min read
Object Oriented Programming(OOP) Series: Traits
A

I’m a Software Engineer with over 5+ years of experience in Technology with a track record in building web applications, mobile applications and Technical Writing. Readily available to explore innovations in ICT and creatively use them to build solutions. Advancing my career in software engineering and contributing to the growth of tech communities around the world. I’m also passionate about working with organizations especially tech, security experts and airline industries to learn and also make contributions that will be a legacy.

Tech is Easy to Learn - https://amazon.com/dp/B0B8T838K

In this OOP concepts series using PHP, Javascript, and Dart we've covered Classes & Objects , Attributes & Methods, Constructor & Destructor, Access modifier, Inheritance, Polymorphism, Abstraction, Encapsulation, Namespaces

A trait is a distinguishing character.

Traits in the context of object-oriented programming are classes used to declare methods with any access modifier that can be used in multiple classes. It cannot be instantiated just like an interface but can be used within a class.

Trait in PHP

PHP doesn't support multiple inheritances but there are cases where for the purposes of reusing codes in several parts of an application trait comes into play.

For instance:

//this doesn't work

Class A extends B, C{
}

The keyword trait is used in PHP

<?php
trait Tutorial{
//write the methods with any access modifier
}
?>

The keyword use is used to access trait(s) within a class.

<?php
trait tutorial {
public function trait_message() {
    echo "I\'m a trait in PHP";
  }
}

class Introduction {
 //since trait is not a class we can only access it but can't instantiate it
  use tutorial;
}

$trait_obj = new Introduction();
$trait_obj->trait_message();
?>

image.png

Output

image.png

We can also access multiple traits from within a class and make use of their methods similar way shown above:

class Introduction {
  use tutorial, phptutorial;
}

In situations where there are conflicts of methods from different traits we can use an alias to differentiate the methods:

use tutorial, phptutorial{
        phptutorial::message as phpMessage;
    }

Traits in Javascript & Dart

There is no direct implementation of traits in Javascript and Darts as in the case of C#, PHP, or RUST.

However, there are approaches that are used to allow multiple methods to be used within a class.

Traitjs can be used in Javascript that supports trait behavior. This [article] (https://www.barbarianmeetscoding.com/blog/safer-javascript-object-composition-with-traits-and-traits-dot-js) provides an example of the implementation

The essence of this tutorial is to explain the trait concepts.

Thank you for reading this article.

Stay tuned for the next article on the OOP Series: Dependency Injection

Please kindly share with your network and feel free to use the comment section for questions, answers, and contributions.

Do you love this article?? please follow me on hashnode or Twitter @alemsbaja to stay updated for more on this OOP series