Programming Languages PHP Subjective
Sep 25, 2025

What are PHP traits?

Detailed Explanation

Traits are a mechanism for code reuse in PHP. They allow you to include methods in multiple classes without using inheritance.\n\nExample:\ntrait Logger {\n public function log($message) {\n echo "Log: $message";\n }\n}\n\nclass User {\n use Logger;\n}\n\nclass Product {\n use Logger;\n}\n\n$user = new User();\n$user->log("User created"); // Works!\n\nTraits solve the problem of multiple inheritance and promote code reuse.

Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback