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_Object_Constants - Fatal编程技术网

PHP-常量对象数组

PHP-常量对象数组,php,object,constants,Php,Object,Constants,我有一个对象数组。如下面的代码所示: Class fileHeadFootBlock { public $offset; public $id; public $length; public $version; } $FILE_HEADER = array(); // This array contains all file header block constant //race1UP file vers

我有一个对象数组。如下面的代码所示:

Class fileHeadFootBlock {
        public $offset;
        public $id;
        public $length;
        public $version;
    }

    $FILE_HEADER = array(); // This array contains all file header block constant
    //race1UP file version
    $FH_FILEVER = new fileHeadFootBlock();
    $FH_FILEVER->offset = 18;
    $FH_FILEVER->id = hexdec('FF');
    $FH_FILEVER->length = hexdec('3E');
    $FH_FILEVER->version = 'File ver: v.20120521 beta (gen from matlab)';
    array_push($FILE_HEADER, $FH_FILEVER);
    //race1UP hardware version
    $FH_RERHWVER = new fileHeadFootBlock();
    $FH_RERHWVER->offset = $FH_FILEVER->offset + $FH_FILEVER->length;
    $FH_RERHWVER->id = hexdec('FE');
    $FH_RERHWVER->length = hexdec('60');
    $FH_RERHWVER->version = 'HARDWARE VERSION: v.20120521 beta.';
    array_push($FILE_HEADER, $FH_RERHWVER);
FileHeadFoodBlock类型的对象数组是$FILE\u头

问题如下: 使$FILE_头保持不变的最佳方法是什么?数组的每个元素及其字段($offset,$id.$length,$version)都应该是共因数

问候,,
Alex

如果您想保留您的数据,则无法更改您的数据文件。您可以将它们设置为私有的,并且只能在构造函数中设置

例如:

Class fileHeadFootBlock {
    private $offset;
    private $id;
    private $length;
    private $version;

    public function __construct($offset, $id, $length, $version)
    {
        $this->offset = $offset;
        $this->id = $id;
        $this->length = $length;
        $this->version = $version;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return mixed
     */
    public function getOffset()
    {
        return $this->offset;
    }

    /**
     * @return mixed
     */
    public function getVersion()
    {
        return $this->version;
    }

    /**
     * @return mixed
     */
    public function getLength()
    {
        return $this->length;
    }
}

$FILE_HEADER = array();
//race1UP file version
$FH_FILEVER = new fileHeadFootBlock(18, 'FF', '3E', 'File ver: v.20120521 beta (gen from matlab)');
array_push($FILE_HEADER, $FH_FILEVER);
//race1UP hardware version
$FH_RERHWVER = new fileHeadFootBlock($FH_FILEVER->getOffset() + $FH_FILEVER->getLength(), 'FE', '60', 'HARDWARE VERSION: v.20120521 beta.');
array_push($FILE_HEADER, $FH_RERHWVER);

如果您想保留您的数据,请不要更改。您可以将它们设置为私有的,并且只能在构造函数中设置

例如:

Class fileHeadFootBlock {
    private $offset;
    private $id;
    private $length;
    private $version;

    public function __construct($offset, $id, $length, $version)
    {
        $this->offset = $offset;
        $this->id = $id;
        $this->length = $length;
        $this->version = $version;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return mixed
     */
    public function getOffset()
    {
        return $this->offset;
    }

    /**
     * @return mixed
     */
    public function getVersion()
    {
        return $this->version;
    }

    /**
     * @return mixed
     */
    public function getLength()
    {
        return $this->length;
    }
}

$FILE_HEADER = array();
//race1UP file version
$FH_FILEVER = new fileHeadFootBlock(18, 'FF', '3E', 'File ver: v.20120521 beta (gen from matlab)');
array_push($FILE_HEADER, $FH_FILEVER);
//race1UP hardware version
$FH_RERHWVER = new fileHeadFootBlock($FH_FILEVER->getOffset() + $FH_FILEVER->getLength(), 'FE', '60', 'HARDWARE VERSION: v.20120521 beta.');
array_push($FILE_HEADER, $FH_RERHWVER);

为什么它们必须是常量?我解决了让它成为私有成员为什么它们必须是常量?我解决了让它成为私有成员