1

I want to use a YAML configuration file, for that, I download symfony/yaml package using composer.

I create my test YAML file content:

testing:
  test: 2

and to parse it I use this code :

$yaml = Yaml::parse(file_get_contents('test.yml'));
var_dump($yaml->testng);

But, I get this error 'Trying to get property 'testng' of non-object' because the parse return array and I want to get an object.

I tried to add (object) but it works just for the first attribute.

Is there any solution?

2
  • This may help, but it looks like the yaml needs to be in Yaml::DUMP_OBJECT format symfony.com/doc/current/components/… Commented Apr 16, 2019 at 4:53
  • There seems to be something very wrong if your error message indicates testng causes an error when that string is not even in your input YAML. Commented Apr 16, 2019 at 5:35

2 Answers 2

6

According to documentation you should just pass Yaml::PARSE_OBJECT_FOR_MAP as a second parameter of Yaml::parse():

$yaml = Yaml::parse(file_get_contents('test.yml'), Yaml::PARSE_OBJECT_FOR_MAP);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply sir
0

you can use this function

  public function toObject($array) {
        $obj = new stdClass();
        foreach ($array as $key => $val) {
            $obj->$key = is_array($val) ? $this->toObject($val) : $val;
        }
        return $obj;
    }

Comments

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.