0

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

}
3
  • Why are you json_decoding a json_encoded array ? If this is your main problem, you may want to know that json_decode can take a second argument to true, and then returns an associative array (php.net/manual/en/function.json-decode.php). Commented Mar 3, 2015 at 17:01
  • @Niols, It's not my main concern, I need to create an object with multiple values according to above dump and the class. I don't need to use json_encode and I did it as a temporary solution. Commented Mar 3, 2015 at 17:04
  • Then Steve's solution might be what you were looking for. By the way, I don't know if "Graph" is the good name, since your object looks more like an edge (but that's not the question here). Commented Mar 3, 2015 at 17:07

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Brian, I need object with muliple values like given in above dump.
1

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

4 Comments

I just want an object consist of multiple Graph object and not stdClass object
@MDeSilva Well i descibe how to create a stdClass object containing multiple Graph objects in my edit. If you dont want the 'parent' object to be a stdClass either, then you would need to create a class for that as well. Why exactly do you want an object of objects? An array of Graph objects seems more usefull
I have another class which iterates through each graph object.that's why
@MDeSilva right, so is my edit a suitable solution? If not please explain why not. Also, iterating an array makes more sense than iterating the properties of an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.