trait ਇੱਕ ਵਿਧੀ ਹੈ ਕੋਡ ਨੂੰ ਮਲਟੀਪਲ ਕਲਾਸਿਜ਼ ਵਿਚਕਾਰ ਦੁਬਾਰਾ ਵਰਤਣ ਲਈ ਜੋ ਵਿਰਸੇ ਦੁਆਰਾ ਜੁੜੀਆਂ ਨਹੀਂ ਹਨ। ਕਿਉਂਕਿ PHP ਸਿਰਫ ਸਿੰਗਲ ਇਨਹੇਰਿਟੈਂਸ ਦੀ ਆਗਿਆ ਦਿੰਦਾ ਹੈ، ਤਾਂ traits ਮੇਥਡਜ਼ (ਅਤੇ ਪ੍ਰੋਪਰਟੀਜ਼) ਨੂੰ ਸ਼ੇਅਰ ਕਰਨ ਦਾ ਕਰ ਪ੍ਰਦਾਨ ਕਰਦੇ ਹਨ — "compositional" ਕੋਡ ਦੁਬਾਰਾ ਵਰਤਣ ਦਾ ਇੱਕ ਰੂਪ، ਜਿਵੇਂ mixins۔
ਸਮੱਸਿਆ ਜੋ traits ਹੱਲ ਕਰਦੇ ਹਨ
PHP has single inheritance — a class can extend only ONE parent. But you often want
to share the SAME methods across unrelated classes (e.g. logging, timestamps).
Duplicating the code is bad; traits let you reuse it without inheritance.
