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

时间:2015-03-03 16:57:55

标签: php oop

以下是我用于创建测试对象的方式。

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

}

2 个答案:

答案 0 :(得分:4)

您的Graph类需要构造函数来设置其属性,然后您可以使用new关键字构建它的实例

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

然后您可以通过以下方式构建图形对象:

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

答案 1 :(得分:1)

创建一个迭代数组的函数,并为每个元素创建一个新的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)
        ));

注意,如果你真的想要一个带有'图表'的stdclass对象。作为数字命名的方法,你可以使用你的json技巧:

return json_decode(json_encode($temp));

虽然为什么你会想要一个阵列超出我的范围