I have two classes. Both classes use a list of properties, which is identical. This list of properties is 75 lines long. I would like to put it in a separate file or so, which both classes can then access. But I am not able to use include.
How can I make my file shorter and the list of properties more flexible in case of changes?
I am not sure if I brought my point across, so I give an example:
I have class foo and class bar.
The LIST OF FRUIT properties private $apples, private $bananas, and private $grapes are used in both classes. Additionally both classes have some other properties, which are specific to each class.
I would like to do something like this:
class foo
{
private $variable_one
private $variable_two
//DEFINE THE LIST OF FRUIT PROPERTIES HERE
public function blahbla...
}
and with the other file
class foo
{
private $variable_three
private $variable_four
//DEFINE THE LIST OF FRUIT PROPERTIES HERE
public function gibberish...
}
Now because in the future I might expand my LIST OF FRUITS and add pineapples and mangos but get rid of the bananas, it would be convenient to have that list as a file in a separate place, where I can modify it, and any changes made will be adopted by any class, that uses the list of fruit properties.
Also it just helps me to reduce the length of my file… like I said, my list of fruit is currently 75 lines long, it’s rather annoying to have this long blurb at the front of both classes.
I appreciate any input or suggestions on how to achieve these two goals (flexibility and short files).
Thanks a lot!
It sounds like both of these classes should be inheriting from a base class which defines the common properties. Review the PHP manual on object inheritance.