首页 > 解决方案 > 如何在php中声明嵌套对象

问题描述

嘿,它尝试将简单对象从 js 转换为 php 并得到错误。

这是对象数组:

    "signatures": [
{
        "id": 5,
        "flowID": 1,
        "fileID": 1,
        "type": "partner",
        "page": 1,
        "position": {
            "x": 444,
            "y": 111
        },
        "size": {
            "width": 555,
            "height": 321
        }
    }, {
        "id": 6,
        "flowID": 1,
        "fileID": 1,
        "type": "partner",
        "page": 1,
        "position": {
            "x": 444,
            "y": 111
        },
        "size": {
            "width": 555,
            "height": 321
        }
    }]

我转换后的php代码:

class Signatures
{
  public $id;
  public $flowID;
  public $fileID;
  public $type;
  public $page;
  public $position;
  public $size;
}

$signature = new Signatures();
$signature->id = 5;
$signature->flowID = 1;
$signature->fileID = 1;
$signature->type = 'partner';
$signature->position->x = 50;
$signature->position->y = 50;
$signature->size->height = 200;
$signature->size->width = 200;

如何使嵌套在php中的位置和大小?我尝试创建新类,但我无法创建“大小”和“位置”属性的问题。感谢这是我第一次使用 php。

错误:

Warning: Creating default object from empty value in /Users/baruchmashasha/Sites/index.php on line 25

Warning: Creating default object from empty value in /Users/baruchmashasha/Sites/index.php on line 27

标签: javascriptphp

解决方案


您需要为位置和大小字段创建复杂的类。

通用概念类

class Point {
    public $x; // int
    public $y; // int

    function __construct() {
        $this->x = 0;
        $this->y = 0;
    }

    public static function create($x, $y) {
        $instance = new self();
        $instance->x = $x;
        $instance->y = $y;
        return $instance;
    }
}

class Rectangle {
    public $width;  // int
    public $height; // int

    function __construct() {
        $this->width = 0;
        $this->height = 0;
    }

    public static function create($width, $height) {
        $instance = new self();
        $instance->width = $width;
        $instance->height = $height;
        return $instance;
    }
}

数据传输对象

class Signature {
    public $id;       // int
    public $flowID;   // int
    public $fileID;   // int
    public $type;     // string
    public $page;     // int
    public $position; // Point
    public $size;     // Rectangle

    function __construct() {
        $this->id = 0;
        $this->flowID = 0;
        $this->fileID = 0;
        $this->type = NULL;
        $this->page = 0;
        $this->position = new Point();
        $this->size = new Rectangle();
    }
}

class SignatureHolder {
    public $signatures; // of Signature

    function __construct() {
        $this->signatures = array();
    }
}

用法

$signatureHolder = new SignatureHolder();

$signature1 = new Signature();
$signature1->id = 5;
$signature1->flowID = 1;
$signature1->fileID = 1;
$signature1->type = 'partner';
$signature1->position = Point::create(444, 111);
$signature1->size = Rectangle::create(555, 321);
array_push($signatureHolder->signatures, $signature1);

$signature2 = new Signature();
$signature2->id = 6;
$signature2->flowID = 1;
$signature2->fileID = 1;
$signature2->type = 'partner';
$signature2->position = Point::create(444, 111); // or you can set x and y
$signature2->size = Rectangle::create(555, 321); // or you can set width and height
array_push($signatureHolder->signatures, $signature2);

echo '<pre>' , var_dump($signatureHolder) , '</pre>'

输出

object(SignatureHolder)#1 (1) {
  ["signatures"] => array(2) {
    [0] => object(Signature)#2 (7) {
      ["id"] => int(5)
      ["flowID"] => int(1)
      ["fileID"] => int(1)
      ["type"] => string(7) "partner"
      ["page"] => int(0)
      ["position"] => object(Point)#5 (2) {
        ["x"] => int(444)
        ["y"] => int(111)
      }
      ["size"] => object(Rectangle)#3 (2) {
        ["width"] => int(555)
        ["height"] => int(321)
      }
    }
    [1] => object(Signature)#4 (7) {
      ["id"] => int(6)
      ["flowID"] => int(1)
      ["fileID"] => int(1)
      ["type"] => string(7) "partner"
      ["page"] => int(0)
      ["position"] => object(Point)#8 (2) {
        ["x"] => int(444)
        ["y"] => int(111)
      }
      ["size"] => object(Rectangle)#6 (2) {
        ["width"] => int(555)
        ["height"] => int(321)
      }
    }
  }
}

推荐阅读