[For more on this, see StackOverflow post SO #16361048 (or for PORTUGUESE]


Dynamically generating codes from arrays

I’m trying to find a way to create a dynamically generating class.
I have an array with of stdClass Objects. Every object in the array has a title, a body, an id, and an image.
I have written a function that returns a template code for the class:
public static function getClass($title, $body, $id, $image){
$class = “class {$title} {$body} {$id}”;
$class.= “image: url(‘{$image}’)”;
return $class;

I am using this template to generate all the code for a class. I have written a function for this too.
private static function generateClass($title, $body, $id){
$class = “class {$title} {$body} {$id}”;
$class.= “image: url(‘{$title}’);”;
$class.= “image: url(‘{$body}’);”;

return $class;

I am then using the function to loop through all the classes to append them to the project:
foreach($classes as $class){
$class = Static::getClass($title, $body, $id, $image);

$html = $template->toHtml();
echo $html;

So far so good. The problem is, I want to be able to make it dynamic. I don’t want to have to loop through the array of objects and do the above for every object.
For example, I have a class “Product” that has title, description, price, and image.
I want to be


Deixe um comentário