Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php Long list of properties in a class - can I shorten it?

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!

like image 986
olli Avatar asked Dec 08 '25 14:12

olli


2 Answers

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.

// The base class defines common properties
class FooBase {
  // protected properties will be available to extending classes
  protected $apples;
  protected $bananas;
  protected $oranges;
}
// Foo extends FooBase, inheriting its protected & public properties
class Foo extends FooBase {
  private $variable_one;
  private $variable_two;

  public function __construct() {
    // Initialize some stuff
    $this->apples = 3;
  }
  public function getApples() {
    // $this->apples inherited from FooBase
    echo $this->apples;
  }
}
// Bar also extends FooBase, and inherits the same 3 properties
class Bar extends FooBase {
  private $variable_three;
  private $variable_four;

  public function __construct() {
    $this->oranges = 9;
  }
  public function getOranges() {
    echo "I have {$this->oranges} oranges too!";
  }
}
like image 153
Michael Berkowski Avatar answered Dec 11 '25 04:12

Michael Berkowski


You can use inheritance:

class FruitObject {
    protected $apples;
    protected $bananas;
    // ...
}

class Foo extends FruitObject {
    private $var1;
    private $var2;
}

class Bar extends FruitObject {
    private $var1;
    private $var2;
}
like image 44
nathan Avatar answered Dec 11 '25 04:12

nathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!