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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:16:40+00:00 2026-05-20T10:16:40+00:00

Somehow my mind went far away from the current problem and I made a

  • 0

Somehow my mind went far away from the current problem and I made a terrible mistake..
I called a parent constructor inside a method that just initializes classes properties.. Or did I.. The parent constructor’s job was to set the ID value. Well PHP allowed me to do that. But isnt that just wrong? And it looks like I can call classes own constructor in the same way.. Isn’t it that constructors should only be allowed to call when creating instances of a class… And they are only called when creating instances..

<?php

    class A {

        public function __construct() {
            echo "Test<br />";
        }

    }

    class B extends A {

        public function test() {
            parent::__construct();
        }

    }

    $b = new B();
    $b->test();

    // OUTPUT:
    // Test
    // Test

?>

EDIT: So the conclusion is that PHP allows you to call the constructor inside a method but it actually does nothing. And that other “TEST” string comes from the base class’s constructor.

  • 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-20T10:16:41+00:00Added an answer on May 20, 2026 at 10:16 am

    One word describes the situation exactly.

    Override

    What happens is that when the parent has a method such as a __construct and the child class does not have that method, when the method is called on the child class it calls the parents method.

    For example:

    class Parent
    {
         public function start(){}
    }
    class Child extends Parent
    {
    
    }
    

    if I call the the start method on the Child class its executes the Parent::Start(), but if Child class has a method called Start() that one would be called as its Overriding the parents version.

    Every class has a constructor method, and php executes that to compile the class into an object, now if you have a method called __construct() in a class, PHP Calls the internal construct which compiles the class, and then calls your override constructor.

    If you do not have a __construct method in the Child class PHP executes the parent __construct

    This is the reason why you get 2 x Test in your Object initialization.

    The only time you should be using parent:__cosntruct() is if your Child class requires a user defined constructor, you explicitly call the parent construct to prepare the parent object.

    For example:

    class Parent
    {
         public function __construct()
         {
             //Do work on Parent Class
         }
    }
    
    class Child extends Parent
    {
        public function __construct()
        {
             //Do Work on Child Class
    
             parent::__construct();
        }
    }
    

    The reason you should only ever call a parent constructor within a child constructor and not a child method is that constructors should only ever be called once, this way enforces that.

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

Sidebar

Related Questions

somehow I get never any results when I call: select * from table_1 t1
Today, during some other somehow related work, came across my mind the following question:
I'm having a problem with the onActivityResult() method not being called. I suspect this
Seems like a simple problem: I have an SVN repo inside our firewall. I
I'm running into a problem starting other activities from mine. I know this must
My mind is somehow blank. How do I do this: I have a RegistrationController
My mind somehow is stuck in a loop of errors. I don't want to
Somehow my FireFox 2 got corrupted, and the status bar is about 3/4 above
Somehow Visual Studio search has stopped working for me. Anytime I search Entire Solution
Somehow I had the impression that ASP.Net differentiates URLs based on the number 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.