Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Traits-in-Php

What is a traits in PHP and how to use it ?

Aman Jain, June 20, 2021November 8, 2023

Before PHP 5.4, there was no way to implement multiple inheritance in PHP then PHP 5.4 introduce traits as a substitute of multiple inheritance in PHP.

By using the traits PHP solve the problem of Multiple Inheritance. A class only extend one class what if we wanted to use multiple classes method in a class ?

Problem of using multiple classes solved by TRAITS in PHP, it enables reuse of methods in several classes without any dependencies.

Syntax of traits:

<?php 
trait name_of_trait{
 // method goes here
}

As we can see it start with trait rather than a class keyword also we can not extend it like regular classes.

Example of Trait:

Using the above trait in a class

<?php 
trait test{

 public function mytest(){
  $this->parentClassVarible=1; // we can change or access parent properties
  $this->changeTrait();
 }

 public function changeTrait(){
   // code goes here
 }

}

If you are using class autoloader and composer then autoload will load the class otherwise you need to include the trait so Using the trait in class

<?php 
include 'test.php';
class Test{

use test; // we can use namespace or include the trait file if it not loaded with      //autoload
private $parentClassVarible=0;
 public function testCls(){

  //we can use trait methods here as well
  $this->mytest();

 }

}

We can also call outside of class a trait using parent class

$test= new Test();
$test->mytest(); // method if traits called using parent instance


We can not create object of a trait:

$testtrait = new test(); // it will generate error 

Thanks for spending time to learn about traits in PHP, you can comment your thoughts and doubts in comment.

Implement multiple inheritance in php using traits

Let’s create a one more trait with name test2

<?php 
trait test2{

 public function anotherTest(){
  $this->parentClassVarible=2; // we can change or access parent properties
  
 }
}

Then using the both traits in class TestClass.php

<?php 
include 'test.php';
include 'test2.php';
class Test{

use test,test2; // we can use namespace or include the trait file if it not loaded with      //autoload
 private $parentClassVarible=0;
 public function testCls(){

  //we can use trait methods here as well
  $this->$parentClassVarible;
  $this->mytest();
  $this->anotherTest();
 }

}

Output of above example:

0
2
1

Related

Softwares Laravel Php multi inhertancephptraits

Post navigation

Previous post
Next post

Related Posts

Php Laravel Custom Facade with example

How to create a custom facade with example in Laravel 8 ?

December 22, 2021January 10, 2022

In Laravel there several facades to use like DB, URL, Validator ,Request etc. Facades are used to use the class methods statically using the application service container. In this article we will learn to create our own facades with service container. Let’s start with step by step For a better…

Read More
Php How to Show Success and Error Flash Message in Laravel

How to Show Success and Error Flash Message in Laravel ?

June 27, 2022November 7, 2023

In this article we will learn to show flash success and error message in laravel. Flash messages are useful when we want to notify the user about their recent activity like submit a form or making any action on website so in this case it may multiple type of messages…

Read More
Javascript Laravel Customized Autocomplete JQuery UI

Laravel Customized Autocomplete JQuery UI

July 12, 2022July 12, 2022

Laravel Customized Autocomplete JQuery UI is useful when we want live search of bulk data. Autocomplete search is mostly work of javascript and when we want to fetch live data from database then we require the intervention of laravel to provide the data in json response. In this tutorial we…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version