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 multiple orderBy clause with example

Laravel multiple orderBy clause with example

October 6, 2022March 16, 2024

In this blog post, we will take a look at how to use the Laravel multiple orderBy clause in the Eloquent ORM. You can order query results by multiple columns using the orderBy method. This is useful when you want to sort by multiple criteria. In this post, we’ll show…

Read More
Php How to Get Current Url with Query Params in Laravel

How to Get Current Url with Query Parameters in Laravel ?

August 27, 2022March 16, 2024

Sometimes in our application we want to Get Current Url with Query Parameters in Laravel. It can be complete url, route name and query params too so that we can fetch the data accordingly and show to user on basis of conditions in our view or controller. We can easily…

Read More
Php How to create migration in laravel

How to Create Migration in Laravel ?

February 15, 2022February 22, 2022

Migrations are used to manage the version control among the developers so that they can sync database schema to each other. Laravel itself provides database migration capabilities to manage database schema updates like create table, alter, modify, drop etc. Main advantage of migrations are to maintain the same schema across…

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

  • August 2025
  • 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

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version