<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pradeep Singh Rajpurohit</title>
    <description>The latest articles on DEV Community by Pradeep Singh Rajpurohit (@rajpurohit_pradeep).</description>
    <link>https://dev.to/rajpurohit_pradeep</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1321167%2F26718697-45f7-43d4-a8f7-98c3cef54099.jpg</url>
      <title>DEV Community: Pradeep Singh Rajpurohit</title>
      <link>https://dev.to/rajpurohit_pradeep</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rajpurohit_pradeep"/>
    <language>en</language>
    <item>
      <title>Traits In PHP</title>
      <dc:creator>Pradeep Singh Rajpurohit</dc:creator>
      <pubDate>Tue, 12 Mar 2024 16:46:20 +0000</pubDate>
      <link>https://dev.to/rajpurohit_pradeep/traits-in-php-48lk</link>
      <guid>https://dev.to/rajpurohit_pradeep/traits-in-php-48lk</guid>
      <description>&lt;p&gt;PHP only supports single inheritance, but with the use of traits, we can achieve multiple inheritance.&lt;/p&gt;

&lt;p&gt;A trait in PHP is like a set of reusable functions that you can add to different classes. It's a way to share code between classes without using regular inheritance.&lt;/p&gt;

&lt;p&gt;Lets take an example:-&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Declare Trait&lt;/strong&gt;&lt;br&gt;
   We can declare a trait with the &lt;code&gt;trait&lt;/code&gt; keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
trait Trait1 {
    public function method1() {
        echo "Method 1 from Trait1!";
    }
}

trait Trait2 {
    public function method2() {
        echo "Method 2 from Trait2!";
    }
}
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above we have declared two traits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Use of Trait&lt;/strong&gt;&lt;br&gt;
   To use a trait in a class, use the &lt;code&gt;use&lt;/code&gt; keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
trait Trait1 {
    public function method1() {
        echo "Method 1 from Trait1!\n";
    }
}

trait Trait2 {
    public function method2() {
        echo "Method 2 from Trait2!\n";
    }
}

class MyClass {
    use Trait1, Trait2;
}

$obj = new MyClass();
$obj-&amp;gt;method1(); // Output: Method 1 from Trait1!
$obj-&amp;gt;method2(); // Output: Method 2 from Trait2!
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope You understand the concept of traits.&lt;br&gt;
Thank for Reading.&lt;/p&gt;

</description>
      <category>php</category>
      <category>webdev</category>
      <category>laravel</category>
      <category>oop</category>
    </item>
  </channel>
</rss>
