为什么我不能在 PHP 中重载构造函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2169448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:19:12  来源:igfitidea点击:

Why can't I overload constructors in PHP?

phpoopoverloadingconstructor

提问by tom

I have abandoned all hope of ever being able to overload my constructors in PHP, so what I'd really like to know is why.

我已经放弃了能够在 PHP 中重载我的构造函数的所有希望,所以我真正想知道的是为什么.

Is there even a reason for it? Does it create inherently bad code? Is it widely accepted language design to not allow it, or are other languages nicer than PHP?

甚至有理由吗?它是否会创建本质上不好的代码?不允许它被广泛接受的语言设计,还是其他语言比 PHP 更好?

采纳答案by pestilence669

You can use variable argumentsto produce the same effect. Without strong typing, it doesn't make much sense to add, given default arguments and all of the other "work arounds."

您可以使用可变参数来产生相同的效果。没有强类型,添加没有多大意义,给定默认参数和所有其他“变通办法”。

回答by Alex Weinstein

You can't overload ANY method in PHP. If you want to be able to instantiate a PHP object while passing several different combinations of parameters, use the factory pattern with a private constructor.

你不能在 PHP 中重载任何方法。如果您希望能够在传递多个不同参数组合的同时实例化 PHP 对象,请使用带有私有构造函数的工厂模式。

For example:

例如:

public MyClass {
    private function __construct() {
    ...
    }

    public static function makeNewWithParameterA($paramA) {
        $obj = new MyClass(); 
        // other initialization
        return $obj;
    }

    public static function makeNewWithParametersBandC($paramB, $paramC) {
        $obj = new MyClass(); 
        // other initialization
        return $obj;
    }
}

$myObject = MyClass::makeNewWithParameterA("foo");
$anotherObject = MyClass::makeNewWithParametersBandC("bar", 3);

回答by Dominic Barnes

True overloading is indeed unsupported in PHP. As @Pestilence mentioned, you can use variable arguments. Some people just use an Associative Array of various options to overcome this.

PHP 确实不支持真正的重载。正如@Pestilence 所提到的,您可以使用可变参数。有些人只是使用各种选项的关联数组来解决这个问题。

回答by Codemonkey

For completeness, I'll suggest Fluent Interfaces. The idea is that by adding return $this;to the end of your methods you can chain calls together. So instead of

为了完整起见,我建议使用Fluent Interfaces。这个想法是通过添加return $this;到方法的末尾,您可以将调用链接在一起。所以代替

$car1 = new Car('blue', 'RWD');
$car2 = new Car('Ford', '300hp');

(which simply wouldn't work), you can do:

(这根本行不通),你可以这样做:

$car = (new Car)
       ->setColor('blue')
       ->setMake('Ford')
       ->setDrive('FWD');

That way you can pick exactly which properties you want to set. In a lot of ways it's similar to passing in an array of options to your initial call:

这样您就可以准确地选择要设置的属性。在很多方面,它类似于将一组选项传递给您的初始调用:

$car = new Car(['make' => 'Ford', 'seats' => 5]);

回答by Anthony Rutledge

PHP Manual: Function Arguments, Default Values

PHP 手册:函数参数,默认值

I have overcome this simply by using default values for function parameters. In __constuct, list the required parameters first. List the optional parameters after that in the general form $param = null.

我只是通过使用函数参数的默认值来克服这个问题。在 中__constuct,首先列出所需的参数。在一般形式之后列出可选参数$param = null

class User
{
    private $db;
    private $userInput;

    public function __construct(Database $db, array $userInput = null)
    {
        $this->db = $db;
        $this->userInput = $userInput;
    }
}

This can be instantiated as:

这可以实例化为:

$user = new User($db)

or

或者

$user = new User($db, $inputArray);

This is not a perfect solution, but I have made this work by separating parameters into absolutely mandatory parametersno matter when the object is constructed, and, as a group, optional parameters listed in order of importance.

这不是一个完美的解决方案,但我通过将参数分成绝对强制参数来完成这项工作,无论对象何时构造,并且作为一个组,按重要性顺序列出可选参数

It works.

有用。

回答by The EasyLearn Academy

<?php
//php do not automatically call parent class constructor at all if child class has constructor so you have to call parent class constructor explicitly, however parent class constructor is called automatically if child class has no constructor
class MyClass 
{
    function construct1($value1)
    {
        echo "<br/> dummy constructor is called with 1 arguments and it is $value1";
    }
    function construct2($value1,$value2)
    {
        echo "<br/> dummy constructor is called with 2 arguments and it is $value1, $value2";
    }
    function construct3($value1,$value2,$value3)
    {
        echo "<br/> dummy constructor is called with 3 arguments and it is $value1, $value2 , $value3";
    } 
    public function __construct()
    {
        $NoOfArguments = func_num_args(); //return no of arguments passed in function
        $arguments = func_get_args();
        echo "<br/> child constructor is called $NoOfArguments";
        switch ($NoOfArguments) {
            case 1:
                 self::construct1($arguments[0]);
                break;
            case 2:
                self::construct2($arguments[0],$arguments[1]);
                break;

            case 3:
                self::construct3($arguments[0],$arguments[1],$arguments[2]);
                break;

            default:
                echo "Invalid No of arguments passed";
                break;
        }
    }


}
$c = new MyClass();
$c2 = new MyClass("ankit");
$c2 = new MyClass("ankit","Jiya");
$c2 = new MyClass("ankit","Jiya","Kasish");

?>

?>

回答by deadManN

they say this work:

他们说这项工作:

<?php
class A
{
    function __construct()
    {
        $a = func_get_args();
        $i = func_num_args();
        if (method_exists($this,$f='__construct'.$i)) {
            call_user_func_array(array($this,$f),$a);
        }
    }

    function __construct1($a1)
    {
        echo('__construct with 1 param called: '.$a1.PHP_EOL);
    }

    function __construct2($a1,$a2)
    {
        echo('__construct with 2 params called: '.$a1.','.$a2.PHP_EOL);
    }

    function __construct3($a1,$a2,$a3)
    {
        echo('__construct with 3 params called: '.$a1.','.$a2.','.$a3.PHP_EOL);
    }
}
$o = new A('sheep');
$o = new A('sheep','cat');
$o = new A('sheep','cat','dog');

// results:
// __construct with 1 param called: sheep
// __construct with 2 params called: sheep,cat
// __construct with 3 params called: sheep,cat,dog
?>

and, it seem every one are happy with it, but for me it didn't work... if you get it to work, its one kind of overloading too...

而且,似乎每个人都对它很满意,但对我来说它不起作用……如果你让它起作用,它也是一种超载……

it take all argoments and pass them to the secondary function constructor...

它接受所有参数并将它们传递给辅助函数构造函数......

回答by DrGeneral

You can use conditional statements in your constructor and then perform your task. Eg.

您可以在构造函数中使用条件语句,然后执行您的任务。例如。

  class Example
  {
      function __construct($no_of_args)

      {// lets assume 2
          switch($no_of_args)
          {
              case 1:
                // write your code
              break;
              case 2:
                //write your 2nd set of code
              break;
              default:
           //write your default statement
         }
      }
   }

    $object1 = new Example(1);  // this will run your 1st case
    $object2 = new Example(2);  // this will run your 2nd case

and so on...

等等...

回答by Mostafa A. Hamid

You can of course overload any function in PHP using __call() and __callStatic() magic methods. It is a little bit tricky, but the implementation can do exactly what your are looking for. Here is the resource on the official PHP.net website:

您当然可以使用 __call() 和 __callStatic() 魔术方法重载 PHP 中的任何函数。这有点棘手,但实现可以完全满足您的要求。这是官方 PHP.net 网站上的资源:

https://www.php.net/manual/en/language.oop5.overloading.php#object.call

https://www.php.net/manual/en/language.oop5.overloading.php#object.call

And here is the example which works for both static and non-static methods:

这是适用于静态和非静态方法的示例:

class MethodTest
{
    public function __call($name, $arguments)
    {
        // Note: value of $name is case sensitive.
        echo "Calling object method '$name' "
             . implode(', ', $arguments). "\n";
    }

    /**  As of PHP 5.3.0  */
    public static function __callStatic($name, $arguments)
    {
        // Note: value of $name is case sensitive.
        echo "Calling static method '$name' "
             . implode(', ', $arguments). "\n";
    }
}

$obj = new MethodTest;
$obj->runTest('in object context');

MethodTest::runTest('in static context');  // As of PHP 5.3.0

And you can apply this to constructors by using the following code in the __construct():

您可以通过在 __construct() 中使用以下代码将其应用于构造函数:

$clsName = get_class($this);
$clsName->methodName($args);

Pretty easy. And you may want to implement __clone() to make a clone copy of the class with the method that you called without having the function that you called in every instance...

挺容易。并且您可能想要实现 __clone() 以使用您调用的方法制作类的克隆副本,而无需在每个实例中调用的函数...

回答by antelove

<?php

    class myClass {

        public $param1 = 'a';
        public $param2 = 'b';

        public function __construct($param1 = NULL, $param2 = NULL) {

            if ($param1 == NULL && $param2 == NULL) {
//                $this->param1 = $param1;
//                $this->param2 = $param2;
            } elseif ($param1 == NULL && $param2 !== NULL) {
//                $this->param1 = $param1;
                $this->param2 = $param2;
            } elseif ($param1 !== NULL && $param2 == NULL) {
                $this->param1 = $param1;
//                $this->param2 = $param2;                
            } else {
                $this->param1 = $param1;
                $this->param2 = $param2;
            }

        }

    }

//    $myObject  = new myClass();
//    $myObject  = new myClass(NULL, 2);
    $myObject  = new myClass(1, '');
//    $myObject  = new myClass(1, 2);

    echo $myObject->param1;
    echo "<br />";
    echo $myObject->param2;

?>