创建一个对象将多个参数传递给 class - PHP

Creating one object passing multiple parameters to a class - PHP

下面是我用来创建用于测试目的的对象的方法。

$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)
            )
        )
    );

//对象的转储

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
        )
)

但现在我需要使用下面的 class 在其他 class 中创建上面的对象。谁能告诉我怎么做?

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

}

你的Graphclass需要一个构造函数来设置它的属性,然后你可以用new关键字构造它的实例

public function __construct($point1, $point2, $value)
{
    $this->setPoint1($point1);
    $this->setPoint2($point2);
    $this->setValue($value);
}

那么您可以通过以下方式构造图对象:

$obj = new Graph("a","b",7);

创建一个函数来迭代数组并为每个元素创建一个新的Graph

class GraphGenerator
{
    static function CreateCollection(array $data)
    {
        $temp=[];
        foreach($data as $item){
            $graph = new Graph();
            $graph->setPoint1($item['point1']);
            $graph->setPoint2($item['point2']);
            $graph->setValue($item['value']);
            $temp[]=$graph;
        }
        return $temp; //if you want array of Graphs
    }

}

$graphArray = GraphGenerator::CreateCollection(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)
        ));

请注意,如果您确实想要一个带有“Graph”作为数字命名方法的 stdclass 对象,您可以使用您的 json 技巧:

return json_decode(json_encode($temp));

虽然我不明白为什么你会想要在数组上使用它