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

Share this:

  • Facebook
  • X

Related

Softwares Laravel Php multi inhertancephptraits

Post navigation

Previous post
Next post

Related Posts

Php Laravel CRUD with Search, Image and Pagination

Laravel CRUD with Search, Image and Pagination

July 1, 2022November 6, 2023

In this article i will learn laravel CRUD with Search, Image and Pagination in laravel. In this post we will not only cover the CRUD operation but also the validation on form, unique validation, image uploading and view the uploaded image, Flash messages, Search the uploaded data and many more….

Share this:

  • Facebook
  • X
Read More
Laravel Laravel 10 Image Upload with Preview Example

Laravel 10 Image Upload: A Comprehensive Tutorial

October 23, 2023November 6, 2023

Laravel 10, renowned for its efficiency and simplicity, offers a seamless solution for handling image uploads in web applications. In Laravel 10 Image Upload tutorial, we will guide you through the process of uploading and displaying images using the robust features of Laravel 10. Whether you are a beginner or…

Share this:

  • Facebook
  • X
Read More
Php How to Copy Folder Recursively One Folder to Another in Laravel

How to Copy Folder Recursively One Folder to Another in Laravel ?

May 26, 2022May 26, 2022

In this article i will help you to copy the folders recursively from one folder to another in laravel .File System is used to perform the file system based operations like copy Folder Recursively. For this purpose laravel uses Illuminate\Filesystem\Filesystem class in laravel. Laravel provides inbuilt library to access the…

Share this:

  • Facebook
  • X
Read More

Leave a ReplyCancel reply

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 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • 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

  • How to Call Controller Method from Another Controller in Laravel ?
  • How to Get Domain Name in Laravel ?
  • How to Append Query String to Route in Laravel ?
  • How to Append URL Query Params to Pagination Laravel ?
  • How to Get Today Records in Laravel ?
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version