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 How to Add Default Value to Column in Laravel Migration

How to Add Default Value to Column in Laravel Migration?

August 17, 2022March 16, 2024

Default value to column is useful when we want to auto fill the default defined value of column during insertion of other values of table So In our this article i will show you to add default value to column in laravel migration. We can add any type of data…

Read More
Php How to Create or Replace File Content in Laravel

How to Create or Replace File Content in Laravel ?

May 29, 2022November 9, 2023

This article will guide you to Check File exist and Create or Replace File Content in Laravel. To perform the file system based operations like Check file exist and create or replace file in laravel. we use File class or Storage class in laravel. Laravel provides inbuilt library to access…

Read More
Php How to assign or declare variable in laravel blade template

How to assign or declare variable in laravel blade template ?

November 8, 2022March 16, 2024

Laravel blade is a powerful templating engine that gives you a lot of control over your HTML. One thing you can declare variable in laravel blade template easily by using different methds. This can be useful if you want to reuse a piece of data in your template, or if…

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

  • 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 Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version