Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for What Is Trait In PHP?
Mohammad ALi Abd Alwahed
Mohammad ALi Abd Alwahed

Posted on

What Is Trait In PHP?

Question asked in every interview: What does trait mean in PHP?
Today's post is about PHP in general and specifically about something we use every day without delving deep into it, which is the trait class. We all know about object-oriented programming and how we can inherit from one class to another. However, sometimes we need to implement multiple inheritance, which means allowing a class to inherit from more than one class. Unfortunately, this feature is not supported in PHP, so we use traits instead.
In the image below, there is an example comparing traits in PHP and the same concept in Python.
In Laravel, all you need to do to use a trait is to start the class definition with the keyword "trait" and import the namespace "App\Traits." Apart from its main purpose, which is solving the problem of multiple inheritances, traits have many other uses, such as in SOLID principles, object-oriented programming, and more.

Image description

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Experienced Software Engineer specializing in the recruitment field, with a robust background in software engineering and SAAS products. Proficient in utilizing Laravel for backend development
  • Location
    Amman , Jordan
  • Education
    UOP
  • Work
    Software Engineer
  • Joined

More fromMohammad ALi Abd Alwahed

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp