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 8041593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:16:24+00:00 2026-06-05T04:16:24+00:00

Alright, so I’m trying to figure out how to structure my code most efficiently.

  • 0

Alright, so I’m trying to figure out how to structure my code most efficiently. I’ve recently switched it from one giant class file with all my methods, to more smaller files that are combined with one base class. This is what I want, however I cannot get it to work properly and I need some help with the structure.

Essentially I need to do this:

  • There will be some functions that are just “tools” for the other classes (such as converter functions, etc.) – they need to be accessible by all the child classes.
  • In some cases “child class one” will need to use functions from “child class two”.
  • A child class needs to be able to set a variable that another child classes can see.

Please let me know how I can get started with these requirements; code examples would be greatly appreciated!

Perhaps some pseudo code will help – but if I’m off please let me know what would work better!

class main {
    private $test;
    public function __construct() {
        //do some stuff
    }
    public function getTest() {
        echo('public call: '.$this->test);
    }
    private function randomFunc(){
        echo('hello');
    }
}
class child1 extends main {
    public function __construct() {
        parent::$test = 'child1 here';
    }
}
class child2 extends main {
    public function __construct() {
        echo('private call: '.parent::$test); //i want this to say "private call: child1 here"
        parent::randomFunc();
    }
}
$base = new main;
new child1;
new child2;
$base->getTest();

So I want the result of that to be:

private call: child1 here
hello
public call: child1 here

So far what I’ve tried doesn’t work… Please help! Thank you.

  • 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-05T04:16:26+00:00Added an answer on June 5, 2026 at 4:16 am

    First point:

    For a Helper class you can choose to make your methods static, so you don’t need an instance:

    class Helper
    {
      public static function usefulMethod()
      {
         // do something useful here
      }
    }
    

    That is now accessible from everywhere like this:

    Helper::usefulMethod()
    

    The next points need some further explanation, which I added as comments to your pseudo code:

    // always start your class names with upper case letter, it's a good convention
    class Main
    {
        // don't make this private if you want to access from the child classes too
        // private $test;
    
        // instead make it protected, so all sub classes can derive
        protected $test;
    
        public function __construct()
        {
            //do some stuff
        }
    
        public function getTest()
        {
            echo 'public call: '.$this->test;
        }
    
        private function randomFunc()
        {
            echo 'hello';
        }
    }
    
    class Child1 extends Main
    {
        public function __construct()
        {
            // $test is now derived from the parent into the sub class
            $this->test = 'child1 here';
        }
    }
    
    
    class Child2 extends Main
    {
        public function __construct()
        {
            // this doesn't quite work like expected:
            // echo 'private call: '.$this->test; //i want this to say "private call: child1 here"
    
            // because this isn't a reference, but think of every class instance
            // as a container which holds its own properties and methods (variables and functions)
            // if you really want to do it like intended then you would need a subclass from main1
            // but that is kind of strange, I don't know exactly what you want to achieve
    
            // this will print out 'hello' as expected
            parent::randomFunc();
        }
    }
    

    I am sure this doesn’t quite answer everything, but I tried my best.
    Maybe you could propose your intentions a bit further

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, I'm trying to figure out why I can't understand how to do this
Alright I'm pretty new to programming and stuff and I'm now trying to code
Alright I've been debugging the whole day but can't figure out what the problem
Alright, this one's interesting. I have a solution, but I don't like it. The
Alright y'all. I am attempting to write a bit of code that places typographic
Alright, here's what I'm trying to do. I'm attempting to write a quick build
Alright, here is my problem i'm trying to solve. I have an index page
Alright, so I've been trying to implement a simple binary search tree that uses
Alright when I do the code: <script type = text/javascript > document.write((new Date).getTime()); </script>
Alright, so I have 2 buttons inline in my header with an HTML structure

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.