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

Laravel Create database connection in laravel

How to make database connection in Laravel 8 ?

December 14, 2021February 22, 2024

Laravel comes with first party support for several database drivers. Laravel gives much better flexibility in terms of using the RDBMS based databases. Laravel supports MySQL, PostgreSQL, SQLite and SQL Server by default but if you want to make connection with Mongo or other database then you need to install…

Read More
Php Laravel 11 Ajax Image Upload with form with example

Laravel 11 Ajax Image Upload with form with example

July 16, 2024July 16, 2024

In this tutorial, I will demonstrate how to implement Laravel 11 Ajax Image Upload with form. This can be achieved using Laravel’s file and storage providers. In our previous articles, such as How to Upload image in Laravel 11 ? we explained how to upload images without Ajax. However, today…

Read More
Php Laravel sum in eloquent query

Laravel sum in eloquent query with example

January 23, 2022November 10, 2023

In this article we will learn to use aggregate function sum in laravel eloquent and query builder. Laravel itself provide inbuilt method to sum the columns like in MySQL or SQL based database we can do sum of columns using sum aggregate function. Sum method will give you the sum…

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