Selected Reading

PHP - Class/Object trait_exists() Function



The PHP Class/Object trait_exists() function is used to check that a specific trait exists in the current script. Traits are a medium to reuse code in single inheritance in PHP. This function helps you add specific features to your class, but only if certain conditions are met.

Syntax

Below is the syntax of the PHP Class/Object trait_exists() function −

bool trait_exists(string $trait, bool $autoload = true)

Parameters

Below are the parameters of the trait_exists() function −

  • $trait − It contains the name of the trait.

  • $autoload − It is the boolean value which tells that to autoload, if not already loaded.

Return Value

The trait_exists() function returns TRUE if trait exists, and FALSE on failure.

PHP Version

First introduced in core PHP 5.4.0, the trait_exists() function continues to function easily in PHP 7, and PHP 8.

Example 1

In this example, we will create a simple trait and then check if it exists with the help of the PHP Class/Object trait_exists() function.

<?php
   // Define a trait here
   trait MyTrait {
      public function sayHello() {
          echo "Hello!";
      }
   }
  
   if (trait_exists('MyTrait')) {
      echo "Trait MyTrait exists.";
   } else {
      echo "Trait MyTrait does not exist.";
   }
?>

Output

Here is the outcome of the following code −

Trait MyTrait exists.

Example 2

In the below PHP code we will use the trait_exists() function and check for a trait that has not been defined. So in that case the function will return false value.

<?php
   // If trait is not defined
   if (trait_exists('TraitNotPresent')) {
      echo "Trait TraitNotPresent exists.";
   } else {
      echo "Trait TraitNotPresent does not exist.";
   }
?> 

Output

This will generate the below output −

Trait TraitNotPresent does not exist.

Example 3

This PHP code shows how to create reusable static methods with a trait and then integrate them into a class. The example shows how to use characteristics to share functionality across classes.

<?php
   // Created trait Skills 
   trait Skills 
   { 
      // Declared static instance 
      private static $skillInstance; 
      protected $result; 
   
      // Defining static function in trait to Reuse 
      public static function Development() 
      { 
         self::$skillInstance = new static(); 
         // Magic constant __TRAIT__ in PHP 
         // It gets the class name for the static method called. 
         self::$skillInstance->result = get_called_class().' is using '.__TRAIT__; 
         
         return self::$skillInstance; 
      } 
   
   } 
   // Checking if 'Skills' Trait exists 
   if ( trait_exists( 'Skills' ) ) 
   { 
      
      class Execute 
      { 
         // Reusing trait 'Skills' 
         use Skills; 
   
         public function displayMessage( $additionalText ) 
         { 
            return $this->result.$additionalText; 
         } 
      } 
   
   } 
   
   echo Execute::Development()->displayMessage(' successfully!');    
?> 

Output

This will create the below output −

Execute is using Skills successfully!
php_function_reference.htm
Advertisements