Php construct object from string

php — Constructing an object with a variable chain of properties from a string

I am trying to write a function that retrieves specific data via cURL. The problem I am running into is that I want the data retrieved to be resolved within the function itself and then returned, rather than obtaining the curl response via the function and THEN retrieving the data outside of the function.

It’s somewhat difficult to explain, however to put it simply, here is essentially what I have been doing up until now:

$response = getData(); // pretend this retrieves a bunch of decoded JSON data from a website via cURL $data[] = $response->july->value; // this data is retrieved outside of the getData function $data[] = $response->info->folders->data->key 

But what I’d like to do is something like this:

$data = getData([ "[RESPONSE]->july->value", "[RESPONSE]->info->folders->data->key" ]); 

Where [RESPONSE] in the two strings would get replaced with the cURL response within the getData function and just return me the results I want (as $data[0] and $data[1], or however many arguments I pass to the function).

The problem with this is that once I turn the two example arguments above into strings, they are no longer objects — Is there a way to build them back up within the function and retrieve the data I want?

Читайте также:  Result example in java

Источник

php — Dynamically construct object selection based on string

Considering I have a string entity.field_one.field_two.value , how can I take this and construct a selection on an existing PHP object? Based on the string above I want to construct:

$output = $entity->field_one->field_two->value; 

I’m aware that I can use variables like $entity-> but considering I don’t know how many dots there are going to be, I need to find a way to construct the whole thing dynamically.

Answer

Solution:

How to access and manipulate multi-dimensional array by key names / path? shows how to do this for nested array elements. You can use a similar method for object properties.

function get($path, $object) < if (is_string($path)) < $path = explode('.', $path); >$temp =& $object; foreach($path as $key) < $temp =& $temp->; > return $temp; > echo get("field_one.field_two.value", $entity); 

Share solution ↓

Additional Information:

Didn’t find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Similar questions

Find the answer in similar questions on our website.

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

About the technologies asked in this question

PHP

PHP (from the English Hypertext Preprocessor — hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/

Welcome to programmierfrage.com

programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.

Get answers to specific questions

Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.

Help Others Solve Their Issues

Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.

Источник

Оцените статью