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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:08:36+00:00 2026-05-23T15:08:36+00:00

Simply question of theory When building a class that is not going to be

  • 0

Simply question of theory

When building a class that is not going to be used by any other client-coders, what is the best method of execution? Consider the following:

class Test
{
    public function __construct()
    {
        $this->foo();
        $this->bar();
    }

    public function foo(){ //do something }

    public function bar(){ //do something }
}

$test = new Test;

As I know exactly what I want this class to do, and the order it should be done in, I simply call the functions during construction. However The same can be done by doing:

class Test
{
    public function __construct(){}

    public function foo(){ //do something }

    public function bar(){ //do something }
}

$test = new Test();
$test->foo();
$test->bar();

What are the advantages to doing it one way or the other? Be it performance, debugging, etc.

Edit

This is a general purpose question. Looking to hear all possibilities and concerns regarding these two ways of executing methods within a class.

  • 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-05-23T15:08:36+00:00Added an answer on May 23, 2026 at 3:08 pm

    In the 1st way you can make those functions private and use them only within the object itself.

    class Test
    {
        public function __construct()
        {
            $this->foo();
            $this->bar();
        }
    
        private function foo(){ //do something }
    
        private function bar(){ //do something }
    }
    
    $test = new Test; 
    //so:
    $test->foo(); //will throw an error
    

    If you make the functions protected instead of private, any class that inherits from this class would be able to to use the functions (while still not allowing for the functions to be called on the outside)


    With your second option, any object can call those functions outside the class definition, and depending on your code, you might not want that.

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

Sidebar

Related Questions

This is a simple question from algorithms theory. The difference between them is that
Simple question: Is there a class or interface that encapulates the getting of a
To phrase my question as simply as possible, is there a way to create
Disregard this question: I've simply confused <script src=...></script> tag and <script> [some functions] </scipt>
Basically the question could more probably be more accurately asked, how do I simply
I have a simple question. Should I return a byte-array or simply base64 encode
Im sorry for this probably dumm question, but I want to simply open modals
Simple question - How do I convert a string that has been through parseInt
I know that this question belongs to the very early stages of the database
I have kind of a theory question on what happens when you have jQuery

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.