PHP trait_exists() Function Last Updated : 02 Mar, 2021 Comments Improve Suggest changes Like Article Like Report PHP implements a way to reuse code called Traits. The trait_exists() function in PHP is an inbuilt function that is used to check whether a trait exists or not. This function accepts the traitname and autoload as parameters and returns true if trait exists, false if not, null in case of an error. Syntax : trait_exists ( string $traitname , bool $autoload = ? ) : bool Parameters: This function accepts two parameters as mentioned above and described below. $traitname: This parameter contains the name of the trait.$autoload: This parameter is a boolean value that tells whether to autoload, if not already loaded. Return Value: It returns true, if a trait exists.It returns false, if the trait does not exist.It returns null for any error encountered. Example 1: PHP <?php // Created trait Programming trait Programming { // Declared static instance private static $instance; protected $temp; // Defining static function in trait to Reuse public static function Designing() { self::$instance = new static(); // Magic constant __TRAIT__ in PHP // It gets the class name for the static method called. self::$instance->temp = get_called_class().' '.__TRAIT__; return self::$instance; } } // Checking if 'Programming' Trait exists if ( trait_exists( 'Programming' ) ) { class Initializing { // Reusing trait 'Programming' use Programming; public function text( $strParam ) { return $this->temp.$strParam; } } } echo Initializing::Designing()->text('!!!'); ?> Output: Initializing Programming!!! Example 2: PHP <?php // Creating Base class class Base { public function callBase() { echo 'This is base function!'."<br>"; } } // Creating Trait trait myTrait { public function callBase() { parent::callBase(); echo 'This is trait function!'."<br>"; } } // Using myTrait class myClass extends Base { use myTrait; } $myObject = new myClass(); $myObject->callBase(); // Checking if trait exists if(trait_exists( "myTrait")) { echo "\n myTrait exists! \n"; } else { echo "\n myTrait does not exists! \n"; } ?> Output: This is base function! This is trait function! myTrait exists! Reference : https://www.php.net/manual/en/function.trait-exists.php Comment More infoAdvertise with us Next Article PHP trait_exists() Function C coder36 Follow Improve Article Tags : Web Technologies PHP PHP Programs PHP-function Similar Reads PHP | Check if a variable is a function To determine whether a passed argument is a function or not, Few of the most preferred methods are shown below. Using is_callable() Function: It is an inbuilt function in PHP which is used to verify the contents of a variable in called as a function. It can check that a simple variable contains the 3 min read How to check the existence of URL in PHP? Existence of an URL can be checked by checking the status code in the response header. The status code 200 is Standard response for successful HTTP requests and status code 404 means URL doesn't exist. Used Functions: get_headers() Function: It fetches all the headers sent by the server in response 2 min read Difference between isset() and array_key_exists() Function in PHP isset() function The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases. Syntax: boo 2 min read How to check if URL contain certain string using PHP? Given a URL and the task is to check the URL contains certain string or not. The URL are basically the strings. So in order to check the existence of certain strings, two approaches can be followed. The first approach is used to find the sub string matching in a string and second approach is to find 4 min read How to Check if a Value Exists in an Associative Array in PHP? Given an Associative array, the task is to check whether a value exists in the associative array or not. There are two methods to check if a value exists in an associative array, these are - using in_array(), and array_search() functions. Let's explore each method with detailed explanations and code 2 min read Like