Search code examples
phpclassmethodsconstruct

Can I use two constructor method in PHP?


I have two php files: main.class.php form.class.php

in these files there are two class that have same name as their file name and class form extends from main class main class has a constructor How can I set a constructor method for class form?


Solution

  • class Main
    {
        private $foo;
    
        public function __construct($foo)
        {
            $this->foo = $foo;
        }
    }
    
    class Form extends Main
    {
        private $bar;
    
        public function __construct($foo, $bar)
        {
            parent::__construct($foo);
            $this->bar = $bar;
        }
    }