0

Not a code question but a design one.

I have a parent class : Item. From this class, inherits two children classes : Weapon and Armor

The class Item contains generic parameters such as Name, Weight and Price. The class Weapons contains parameters relatives to a weapon (category, damages, range...) The class Armor contains parameters relatives to an armor (bonus to AC, spell failure, speed modifier...)

For now, I can easily create objects that are either a weapon or an armor.

But how can I manage to create an object such as a lantern shield, a buckler or a pata that are simultaneously a weapon and an armor ? I know that I have to change my design to achieve that, but can't find a way to do it.

GregoirePelegrin
  • 1,206
  • 2
  • 7
  • 23
Eldev
  • 21
  • 3
  • I don't think it is possible in PHP. You will need to create new child class for `Weapon` as `ArmoredWeapon` and for `Armor` as `WeaponizedArmor` – Vüsal Hüseynli Nov 25 '22 at 09:20
  • That's what I feared since multi-inheritance isn't allowed in php and traits and interfaces cannot implement private properties. I will then create a specific class for these items. Thank you for your time – Eldev Nov 25 '22 at 09:28
  • Exactly and unfortunately these are main reasons that `PHP` is not the best language. Even you can not `cast` an object to its parent or child class and there is not `Polymorphism(overloading)` in PHP – Vüsal Hüseynli Nov 25 '22 at 09:34
  • 1
    A trait can have private properties: https://stackoverflow.com/questions/47727003/can-traits-have-properties-methods-with-private-protected-visibility-can-tr – Marleen Nov 25 '22 at 09:37
  • This is also the point where you might want to research a bit about composition over inheritance. Defining ArmorInterface and WeaponInterface opens the door to quite a bit of flexibility. And who knows you may end up needing additional interfaces as your design progresses. – Cerad Nov 25 '22 at 13:55
  • This is a faq. Please before considering posting: Pin down code issues via [mre]. Read the manual/reference & google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular names/strings/numbers, 'site:stackoverflow.com' & tags; read many answers. Reflect research in posts. SO/SE search is poor & literal & unusual, read the help. Google re googling/searching, including Q&A at [meta] & [meta.se]. [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/3404097) [ask] [Help] – philipxy Dec 02 '22 at 06:31

0 Answers0