简体   繁体   English

如何在 PHP 的 class 构造函数中将数组作为参数传递

[英]How to pass an array as a parameter in a class constructor in PHP

I'm trying to pass these parameters in my class constructor:我试图在我的 class 构造函数中传递这些参数:

  1. $tableName : Database table name. $tableName :数据库表名。
  2. $id : ID field name of my database table. $id :我的数据库表的 ID 字段名称。
  3. $tableFields : Field names of my database table. $tableFields :我的数据库表的字段名称。

Parameter 1 and 2 always will be only 1, but parameter 3 is variable, maybe 2, maybe 4 or maybe 15.参数 1 和 2 总是只有 1,但参数 3 是可变的,可能是 2、可能是 4 或可能是 15。

So, I need to save it into an array, but I'm not sure how to do it.所以,我需要将它保存到一个数组中,但我不知道该怎么做。

Guys, can you help me to understand this?伙计们,你能帮我理解这个吗?

<?php
    class GenerateCrud {
    
        // Properties.
        
            public $tableName;
            public $id;
            public $tableFields = array();
    
        // Constructor.
        
            function __construct($tableName, $id, $tableFields){
                $this->tableName = $tableName;
                $this->id = $id;
                $this->tableFields[] = $tableFields;
            }
    }
    
    $myObject = new GenerateCrud('users_test', 'id', 'field1', 'field2', 'field3', 'field4');
?>

Add:添加:

$this->tableFields = $tableFields;

And:和:

$myObject = new GenerateCrud('users_test', 'id', ['field1', 'field2', 'field3', 'field4']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM