2

Hey it try to convert simple object from js to php and get errors.

This is the array of object:

    "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
        }
    }]

My converted php code:

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;

How to make to position and size nested in php? I try to create new class but the problem that i cant create properties of "size" and "position". Thanks its my first time on php.

Error:

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
Baruch Mashasha
  • 951
  • 1
  • 11
  • 29

2 Answers2

3

You need to create complex classes for the position and size fields.

Generic concept classes

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;
    }
}

Data transfer objects

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();
    }
}

Usage

$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>'

Output

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)
      }
    }
  }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
1

While Mr Polywhirls answer isn't incorrect, there is a simpler, tiny bit dirtier option available:

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

This isn't persé better/worse, just different. I'd say that this kind of solution is suitable for small solutions. If you have a significant class / workload, the solution by Mr Polywhirls is more neat.

Martijn
  • 15,791
  • 4
  • 36
  • 68