带参数的 PHP 构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9346856/
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-26 06:37:41  来源:igfitidea点击:

PHP constructor with a parameter

phpconstructor

提问by Kamil

I need a function that will do something like this:

我需要一个函数来做这样的事情:

$arr = array(); // This is the array where I'm storing data

$f = new MyRecord(); // I have __constructor in class Field() that sets some default values
$f->{'fid'} = 1;
$f->{'fvalue-string'} = $_POST['data'];
$arr[] = $f;

$f = new Field();
$f->{'fid'} = 2;
$f->{'fvalue-int'} = $_POST['data2'];
$arr[] = $f;

When I write something like this:

当我写这样的东西时:

$f = new Field(1, 'fvalue-string', $_POST['data-string'], $arr);
$f = new Field(2, 'fvalue-int', $_POST['data-integer'], $arr);

// Description of parameters that I want to use:
// 1 - always integer, unique (fid property of MyRecord class)
// 'fvalue-int' - name of field/property in MyRecord class where the next parameter will go
// 3. Data for field specified in the previous parameter
// 4. Array where the class should go

I don't know how to make a parametrized constructor in PHP.

我不知道如何在 PHP 中创建参数化构造函数。

Now I use a constructor like this:

现在我使用这样的构造函数:

class MyRecord
{
    function __construct() {
        $default = new stdClass();
        $default->{'fvalue-string'} = '';
        $default->{'fvalue-int'} = 0;
        $default->{'fvalue-float'} = 0;
        $default->{'fvalue-image'} = ' ';
        $default->{'fvalue-datetime'} = 0;
        $default->{'fvalue-boolean'} = false;

        $this = $default;
    }
}

回答by Mike B

Read all of Constructors and Destructors.

阅读所有构造函数和析构函数

Constructors can take parameters like any other function or method in PHP:

构造函数可以像 PHP 中的任何其他函数或方法一样接受参数:

class MyClass {

  public $param;

  public function __construct($param) {
    $this->param = $param;
  }
}

$myClass = new MyClass('foobar');
echo $myClass->param; // foobar

Your example of how you use constructors now won't even compile as you can't reassign $this.

您现在如何使用构造函数的示例甚至无法编译,因为您无法重新分配$this.

Also, you don't need the curly brackets every time you access or set a property. $object->propertyworks just fine. You only need to use curly-brackets under special circumstances like if you need to evaluate a method $object->{$foo->bar()} = 'test';

此外,每次访问或设置属性时都不需要大括号。$object->property工作得很好。你只需要在特殊情况下使用花括号,比如你需要评估一个方法$object->{$foo->bar()} = 'test';

回答by Michael Robinson

If you want to pass an array as a parameter and 'auto' populate your properties:

如果要将数组作为参数传递并“自动”填充属性:

class MyRecord {
    function __construct($parameters = array()) {
        foreach($parameters as $key => $value) {
            $this->$key = $value;
        }
    }
}

Notice that a constructor is used to create & initialize an object, therefore one may use $thisto use / modify the object you're constructing.

请注意,构造函数用于创建和初始化对象,因此可以$this使用/修改您正在构造的对象。