PHP TRAITS

Sugar babies

PHP TRAITS

  • Traits are a mechanism for code reuse in single inheritance languages such as PHP.  
  • A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.
  • A trait, as related to genetics, is a specific characteristic of an individual. 
  • Traits can be determined by genes, environmental factors or by a combination of both.

TYPES OF TRAITS IN PHP :

  • Cardinal traits
  • Central traits.
  • Secondary traits.

CHARACTERISTICS OF TRAITS IN PHP :

  • Loyal
  • Generous.
  • Kind.
  • Sincere.
  • Persistent.
  • Open-minded
  • Brave.
  • Quiet.

SYNTAX FOR TRAITS IN PHP :

trait Greeting
 { 
public function sayHello() 
  }
{
 return "Hello!"; 

EXAMPLE FOR TRAITS IN PHP :

<?php
trait messages 
 {
public function msg1 ( )  
 {
        echo  "OOP is easy topic!  " ;
     }
}

class Welcome 
 {
     use message 1;
}

$obj = new Welcome ( ) ;
$obj->msg1 ( ) ;
?>

OUTPUT :

OOP is easy topic!


Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send