Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8669659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:31:49+00:00 2026-06-12T18:31:49+00:00

I have two classes. Both classes use a list of properties, which is identical.

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:31:51+00:00Added an answer on June 12, 2026 at 6:31 pm

    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!";
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes which both derive from the same parent: public class People{
Say I have a div that uses two css classes that both use text-align,
If I have two classes that both implement an interface, but also inherit, do
Suppose I have two classes that both contain a static variable, XmlTag. The second
I have two classes generated by LINQ2SQL both from the same table so they
Say you have two classes, A and B. Is it possible to instantiate both
I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes, Class A and Class B, both of them are subclasses
I have two classes - Task (which implements Comparable) and DeadlinedTask (where DeadlinedTask extends
I have two classes in my app class A and Class B. Both class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.