Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的可选PHP参数做错了什么?_Php_Class_Constructor - Fatal编程技术网

我的可选PHP参数做错了什么?

我的可选PHP参数做错了什么?,php,class,constructor,Php,Class,Constructor,我在以下课程中有一门课: class MyClass { public function __construct($id = 0, $humanIdentifier = '') { $this->id = $id; $this->humanID = $humanIdentifier; } } 因此,根据我的解释,我应该能够将$id或$humanIdentifier传递给该构造函数,如果我愿意,两者都不能传递。然而,当我调用下面的代码

我在以下课程中有一门课:

class MyClass {

    public function __construct($id = 0, $humanIdentifier = '') {
        $this->id = $id;
        $this->humanID = $humanIdentifier;
    }
}
因此,根据我的解释,我应该能够将$id或$humanIdentifier传递给该构造函数,如果我愿意,两者都不能传递。然而,当我调用下面的代码时,我发现构造函数参数中的$id被设置为hello world,而不是$humanIdentifier,尽管我在调用构造函数时指定了$humanIdentifier。谁能看出我错在哪里

$o = new MyClass($humanIdentifier='hello world');

PHP不支持命名参数,它将根据传递参数的顺序设置值

在您的情况下,您传递的不是
$humanIdentifier
,而是表达式
$humanIdentifier='hello world'
的结果,该表达式后来设置为
$this->id

我知道在PHP中模仿命名参数的唯一方法是数组。因此,您可以(在PHP7中)执行以下操作:


PHP不支持命名参数,它将根据传递参数的顺序设置值

在您的情况下,您传递的不是
$humanIdentifier
,而是表达式
$humanIdentifier='hello world'
的结果,该表达式后来设置为
$this->id

我知道在PHP中模仿命名参数的唯一方法是数组。因此,您可以(在PHP7中)执行以下操作:

谁能看出我错在哪里

$o = new MyClass($humanIdentifier='hello world');
是的,您认为这些是命名参数。事实并非如此。它们是位置参数。所以你可以这样称呼它:

new MyClass(0, 'hello world')
过去曾建议添加对命名参数的支持。一个更新的RFC,但它仍有待改进和实现

谁能看出我错在哪里

$o = new MyClass($humanIdentifier='hello world');
是的,您认为这些是命名参数。事实并非如此。它们是位置参数。所以你可以这样称呼它:

new MyClass(0, 'hello world')

过去曾建议添加对命名参数的支持。更新的RFC,但仍有待改进和实现。

您不能通过这种方式创建类的新对象:

    $o = new MyClass($humanIdentifier='hello world');
$o = new MyClass(['humanId'=>hello world']);
您可以使用数组作为
\u构造的参数

class MyClass {

    public function __construct(array $arg) {
        $this->id = isset($arg['id']) ? $arg['id'] : 0;
        $this->humanID = isset($arg['humanID']) ? $arg['humanID'] : 0;
    }
}
然后可以通过以下方式创建类的新对象:

    $o = new MyClass($humanIdentifier='hello world');
$o = new MyClass(['humanId'=>hello world']);

您不能通过以下方式创建类的新对象:

    $o = new MyClass($humanIdentifier='hello world');
$o = new MyClass(['humanId'=>hello world']);
您可以使用数组作为
\u构造的参数

class MyClass {

    public function __construct(array $arg) {
        $this->id = isset($arg['id']) ? $arg['id'] : 0;
        $this->humanID = isset($arg['humanID']) ? $arg['humanID'] : 0;
    }
}
然后可以通过以下方式创建类的新对象:

    $o = new MyClass($humanIdentifier='hello world');
$o = new MyClass(['humanId'=>hello world']);

您需要重载构造函数,但是php没有内置的功能,但是在文档中有一个很好的解决方法:


这里还讨论了为什么这可能是个坏主意:

您需要重载构造函数,但php没有内置的功能,但文档中有一个很好的解决方法:


这里还讨论了为什么这可能是个坏主意:

正如另一个答案所说,php不支持命名参数。您可以通过以下方式完成类似的任务:

class MyClass {

  public function __construct($args = array('id' => 0, 'humanIdentifier' => '') {.
    // some conditional logic to emulate the default values concept
    if(!isset($args['id'])){
      $this->id = 0;
    }else{
      $this->id = $args['id'];
    }
    if(!isset($args['humanIdentifier'])){
      $this->humanID = '';
    }else{
      $this->humanID = $args['humanIdentifier'];
    }
  }
}
你可以这样称呼它:

new MyClass(array('humanIdentifier'=>'hello world'));

默认的
id
将出现在那里。我相信,如果有足够的参数,您可以想出一些奇特的迭代来实现这一点。

正如另一个答案所说,php不支持命名参数。您可以通过以下方式完成类似的任务:

class MyClass {

  public function __construct($args = array('id' => 0, 'humanIdentifier' => '') {.
    // some conditional logic to emulate the default values concept
    if(!isset($args['id'])){
      $this->id = 0;
    }else{
      $this->id = $args['id'];
    }
    if(!isset($args['humanIdentifier'])){
      $this->humanID = '';
    }else{
      $this->humanID = $args['humanIdentifier'];
    }
  }
}
你可以这样称呼它:

new MyClass(array('humanIdentifier'=>'hello world'));

默认的
id
将出现在那里。我相信,如果有足够的参数,您可以想出一些奇特的迭代来完成这项工作。

这将被解释为将“hello world”分配给名为
$humanIdentifier
的变量,然后将该值作为第一个参数传递(
$id
)这将被解释为将“hello world”赋值给名为
$humanIdentifier
的变量,然后将此值作为第一个参数(
$id
)传递给构造函数。这太完美了,谢谢,我习惯于使用Python,但有一段时间没有使用PHP。非常感谢,我习惯于使用Python,有一段时间没有使用PHP了。