Browse Source

add new function to detect if a class is using a trait

Tim Fry 1 year ago
parent
commit
fba800c9d2
1 changed files with 11 additions and 0 deletions
  1. 11 0
      resources/functions.php

+ 11 - 0
resources/functions.php

@@ -1,5 +1,16 @@
 <?php
 <?php
 
 
+	if (!function_exists('has_trait')) {
+		function has_trait($object_or_class, $trait): bool {
+			if (trait_exists($trait) && in_array($trait, class_uses($object_or_class))) {
+				//class has the trait
+				return true;
+			}
+			//class does not have the trait
+			return false;
+		}
+	}
+
 	if (!function_exists('has_interface')) {
 	if (!function_exists('has_interface')) {
 		/**
 		/**
 		 * Returns true when the class has implemented the interface
 		 * Returns true when the class has implemented the interface