Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.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 将声明的id转换为数组_Php_Wordpress - Fatal编程技术网

Php 将声明的id转换为数组

Php 将声明的id转换为数组,php,wordpress,Php,Wordpress,正在尝试将此行更改为数组 private $formId = '1'; 尝试: private $formId = array('1', '2'); 但它不起作用。 我是一个编程新手,非常感谢任何帮助:)试试看 private $formId = ['1', '2']; 您可以初始化构造函数中的任何类属性。想象一下下面的类: class FormManager{ private $formId; private $formName;

正在尝试将此行更改为数组

private $formId = '1';  
尝试:

private $formId = array('1', '2');
但它不起作用。
我是一个编程新手,非常感谢任何帮助:)

试试看

private $formId = ['1', '2'];

您可以初始化构造函数中的任何类属性。想象一下下面的类:

    class FormManager{
        private $formId;
        private $formName;
        private $formMethod;

        public function __construct() {
            // YOU CAN SET ANY VALUES TO ANY OF THE PROPERTIES OF THIS CLASS
            // HERE SO THAT EACH OBJECT CREATED FROM THIS CLASS WOULD HAVE
            // THE INITIAL VALUES SET HERE WITHIN THE CONSTRUCTOR
            $this->formId     = array('1', '2');
            $this->formMethod = "POST";
            $this->formName   = "base_form";
        }

        /**
         * @return array|null
         */
        public function getFormId() {
            return $this->formId;
        }

        /**
         * @param array|null $formId
         * @return FormManager
         */
        public function setFormId($formId) {
            $this->formId = $formId;

            return $this;
        }

        /**
         * @return null|string
         */
        public function getFormMethod() {
            return $this->formMethod;
        }

        /**
         * @param null|string $formMethod
         * @return FormManager
         */
        public function setFormMethod($formMethod) {
            $this->formMethod = $formMethod;

            return $this;
        }

        /**
         * @return null|string
         */
        public function getFormName() {
            return $this->formName;
        }

        /**
         * @param null|string $formName
         * @return FormManager
         */
        public function setFormName($formName) {
            $this->formName = $formName;

            return $this;
        }
    }
下面是构造函数接受3个参数的另一个变体:

    class FormManager{
        private $formId;
        private $formName;
        private $formMethod;

        /**
         * FormManager constructor.
         * @param $formId
         * @param $formMethod
         * @param $formName
         */
        public function __construct($formId=null, $formMethod=null, $formName=null) {
            // YOU CAN SET ANY VALUES TO ANY OF THE PROPERTIES OF THIS CLASS
            // HERE SO THAT EACH OBJECT CREATED FROM THIS CLASS WOULD HAVE
            // THE INITIAL VALUES SET HERE WITHIN THE CONSTRUCTOR.
            // THE ARGUMENTS HERE ARE OPTIONAL & THEY DEFAULT EACH TO NULL.
            // UNLESS AN ARGUMENT IS SUPPLIED, THE VALUES YOU SET HERE FOR
            // EACH OF THE PROPERTIES OVERRIDE THE NULLS...
            $this->formId     = ( isset($formId)     && !is_null($formId) )         ? $formId       : array('1', '2');
            $this->formMethod = ( isset($formMethod) && !is_null($formMethod) )  ? $formMethod   : "POST";
            $this->formName   = ( isset($formName)   && !is_null($formName) )   ? $formName     : "base_form";
        }

        /**
         * @return array|null
         */
        public function getFormId() {
            return $this->formId;
        }

        /**
         * @param array|null $formId
         * @return FormManager
         */
        public function setFormId($formId) {
            $this->formId = $formId;

            return $this;
        }

        /**
         * @return null|string
         */
        public function getFormMethod() {
            return $this->formMethod;
        }

        /**
         * @param null|string $formMethod
         * @return FormManager
         */
        public function setFormMethod($formMethod) {
            $this->formMethod = $formMethod;

            return $this;
        }

        /**
         * @return null|string
         */
        public function getFormName() {
            return $this->formName;
        }

        /**
         * @param null|string $formName
         * @return FormManager
         */
        public function setFormName($formName) {
            $this->formName = $formName;

            return $this;
        }
    }

使用构造函数,可以初始化
$formId
$formMethod
&
$formName
。请注意,构造函数中的所有参数都具有默认值
NULL
。这是有意的-只是为了演示在对象构造过程中将成员变量初始化为您可能喜欢的任何值的可能性。

您可以将类属性初始化为构造函数。具体需要什么?解释清楚?你会得到什么错误??
private$formId=array('1','2')
是一个有效的表达式,在定义类属性时应该起作用。所以问题在别处…我认为你是对的George,我相信问题在别处。。