Below is the way which I used to create an object for testing purposes.
$graph = (object)json_decode(
json_encode(
array(
array("point1" => "a", "point2" => "b", "value" => 7),
array("point1" => "a", "point2" => "c", "value" => 9),
array("point1" => "a", "point2" => "f", "value" => 14),
array("point1" => "b", "point2" => "c", "value" => 10),
array("point1" => "b", "point2" => "d", "value" => 15),
array("point1" => "c", "point2" => "d", "value" => 11),
array("point1" => "c", "point2" => "f", "value" => 2),
array("point1" => "d", "point2" => "e", "value" => 6),
array("point1" => "e", "point2" => "f", "value" => 9)
)
)
);
//Dump of the object
stdClass Object
(
[0] => stdClass Object
(
[point1] => a
[point2] => b
[value] => 7
)
[1] => stdClass Object
(
[point1] => a
[point2] => c
[value] => 9
)
[2] => stdClass Object
(
[point1] => a
[point2] => f
[value] => 14
)
[3] => stdClass Object
(
[point1] => b
[point2] => c
[value] => 10
)
)
But now I need to use below class to make above object in some other class. Can someone tell how to do it ?
class Graph
{
/**
* @var
*
* starting point of an edge
*/
protected $point1;
/**
* @var
*
* end point of an edge
*/
protected $point2;
/**
* @var
*
* value (distance, time, etc..) between two points
*/
protected $value;
public function getPoint1()
{
return $this->point1;
}
public function setPoint1($point1)
{
$this->point1 = $point1;
}
public function getPoint2()
{
return $this->point2;
}
public function setPoint2($point2)
{
$this->point2 = $point2;
}
public function getValue()
{
return $this->value;
}
public function setValue($value)
{
$this->value = $value;
}
}
json_decodecan take a second argument totrue, and then returns an associative array (php.net/manual/en/function.json-decode.php).