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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:23:57+00:00 2026-05-27T06:23:57+00:00

Sorry, for the badly phrased title. I don’t know much object-oriented PHP, so, I

  • 0

Sorry, for the badly phrased title. I don’t know much object-oriented PHP, so, I couldn’t come up with a better title (or an answer to my problem!).

Ok, so, I have something like this:

Class foo{
    var $hello;
    function foo(){
    }
}

Class customfoo extends foo{
    //No Constructor
}

Now, I wrote another class, let’s call it customclass and I want to use it in the customfoo class. But, I don’t just want to use it in the customfoo class, I want it to be created as soon as the customfoo class is created.

Ordinarily, I presume, you would just use the constructor for this, so something like:

 Class customfoo extends foo{
    var $custom;
    function customfoo(){
        $this->custom = new customclass();   
    }
}

However, customfoo is a child class, so, I think the constructor would replace the constructor of the parent class, and I don’t want that to happen. So, how can I make a class customclass when customfoo is first started? I guess, I could just write an arbitrary function and call it via some other function (that I am sure gets executed early on), but it would be nice to at least know how to do the above.

  • 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-27T06:23:57+00:00Added an answer on May 27, 2026 at 6:23 am

    First, you should name your counstructors as __construct.

    As you have pointed out, the constructor you write will overwrite the parent’s one, but you can always call it from the child class, like this:

    Class customfoo extends foo{
        var $custom;
        function __construct(){
            parent::__construct();
            $this->custom = new customclass();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's a badly worded title but I can't come up with anything better, sorry!
Sorry I couldn't come up with a better title. Here is my htaccess RewriteEngine
Sorry for a badly-phrased question. I have a requirement coming from a non-technical person
Sorry for the basic question - I'm a .NET developer and don't have much
Sorry if the title is poorly descriptive, but I can't do better right now
(sorry about the long title) I have a custom object Person, which in turn
Sorry, I can tell I don't entirely know what I'm talking about. For example,
Sorry the title isn't more help. I have a database of media-file URLs that
Sorry for the long question title. I guess I'm on to a loser on
Sorry for asking such a newbie question, I know it makes a few of

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.