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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:18:55+00:00 2026-06-11T20:18:55+00:00

In PHP 5, I can to overload constructors (and any others methods). But if

  • 0

In PHP 5, I can to overload constructors (and any others methods). But if I get some code like this:

class Base {

    public function __construct($a, $b) {
        echo $a+$b;
    }


    public function sayHello() {
        echo 'Hello ';
    }
}


trait SayWorld {

    public function __construct($a, $b, $c = 0) {
        echo (int)$c * ($a+$b);
    }

    public function sayHello($a = null) {
        parent::sayHello();
        echo 'World!'.$a;
    }
}

class MyHelloWorld extends Base {
    use SayWorld;
}

$o = new MyHelloWorld(2, 3);
$o->sayHello(1);

I have an error:

Fatal error: MyHelloWorld has colliding constructor definitions coming from traits

How can I to fix it? You can test my code here.

  • 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-11T20:18:57+00:00Added an answer on June 11, 2026 at 8:18 pm

    I think for now the only way to do what you want is:

    class MyHelloWorld extends Base {
    
        use SayWorld {
            SayWorld::__construct as private __swConstruct;
        }
    
        public function __construct($a, $b, $c = 0)
        {
            $this->__swConstruct($a, $b, $c);
        }
    }
    

    Edit 2:

    My advice, based on over a year of dealing with traits in PHP, is: avoid writing constructors in traits at all, or if you must – at least make them parameterless. Having them in traits goes against the idea of constructors in general, which is: constructors should be specific to a class to which they belong. Other, evolved high-level languages don’t even support implicit constructor inheritance. This is because constructors have far more stronger relation to the class then other methods. In fact they have so strong relation, that even the LSP does not apply to them. The traits in Scala language (a very mature and SOLID-friendly successor of Java), can’t have a constructor with parameters.

    Edit 1:

    There was a bug in PHP 5.4.11, which actually allowed to alias a superclass method. But this was considered a no-no by the PHP developers, so we are still stuck with that cumbersome solution which I presented above. But that bug raised a discussion about what can be done with this, and I’m hoping it will be targeted in future releases.

    Meanwhile I came across the same problem over and over again. My irritation raised exponentially with the number of parameters and lines of docblock which had to be repeated a lot of times in order to use the trait. So I came up with the following pattern in order to stick to the DRY rule as much as I could:

    Instead of repeating entire set of parameters like this:

    trait SayWorld {
    
        /**
         * This is a valid docblock.
         *
         * @param int $a Doc comment.
         * @param int $b Doc comment.
         */
        public function __construct($a, $b) {
            echo (int)$c * ($a+$b);
        }
    }
    
    class MyHelloWorld extends Base {
    
        use SayWorld {
            SayWorld::__construct as private __swConstruct;
        }
    
        /**
         * Repeated and unnecessary docblock.
         *
         * @param int $a Doc comment.
         * @param int $b Doc comment.
         * @param int $c Doc comment.
         */
        public function __construct($a, $b, $c = 0)
        {
            $this->__swConstruct($a, $b);
        }
    }
    

    I write a class much like a tuple (concept familiar to C# and Python users), and use it instead of an endless list of parameters:

    class SayWorldConstructTuple
    {
        public $a;
    
        public $b;
    
        public function __construct($a, $b)
        {
            $this->a = $a;
            $this->b = $b;
        }
    }
    
    class MyHelloWorld extends Base {
    
        use SayWorld {
            SayWorld::__construct as private __swConstruct;
        }
    
        /**
         * New and valid docblock.
         *
         * @param SayWorldConstructTuple $Tuple
         * @param int $c Additional parameter.
         */
        public function __construct(SayWorldConstructTuple $Tuple, $c = 0)
        {
            $this->__swConstruct($Tuple->a, $Tuple->b);
            $this->c = $c;
        }
    }
    

    Note: this pattern is of course more useful with a larger amount of tuple’s constructor parameters, and more classes using the tuple.

    It can be automated further with the use of PHP’s dynamic nature.

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

Sidebar

Related Questions

how to make php can read log file in /var/log/.... I tried this code
so my index.php can be this: <?php $restOfURL = ''; //idk how to get
Is it possible to check if the php can get some kind of a
e.g in C# you can overload methods using the same method name but what
As far as I know, only PHP can't be used for this. But since
I was wondering if PHP can do this as there seems to be no
How can PHP cause memory leaks, buffer overflows, stack overflows and any other errors
In php i can get today date/day by using : $today = date('D, Y-m-d');
In php how can I read a text file and get each line into
PHP can't recognize 1,200.00 (generated by number_format ) but only 1200.00 , What's the

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.