Shaolin
Shaolin

Reputation: 2551

Creating one object passing multiple parameters to a class - PHP

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

}

Upvotes: 0

Views: 2115

Answers (2)

Steve
Steve

Reputation: 20469

Create a function to iterate the array and create a new Graph for each element:

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

Note if you actually want a stdclass object with the 'Graph's' as numerically named methods, you can use your json trick:

return json_decode(json_encode($temp));

Though why you would want that over an array is beyond me

Upvotes: 1

Brian Glaz
Brian Glaz

Reputation: 15696

Your Graph class needs a constructor function to set its properties, and then you can construct instances of it with the new keyword

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

Then you can construct graph objects in the following way:

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

Upvotes: 4

Related Questions