Skip to content

Instantly share code, notes, and snippets.

@useless-stuff
Last active February 16, 2016 10:03
Show Gist options
  • Save useless-stuff/0fb1fdf97ce577d1812d to your computer and use it in GitHub Desktop.
Save useless-stuff/0fb1fdf97ce577d1812d to your computer and use it in GitHub Desktop.
PHP - RecursiveArrayIterator
<?php
/**
* Class Message
*/
class Message
{
public $title;
public $body;
public $attachments = array();
public function addAttachment($attachment)
{
$this->attachments[] = $attachment;
}
}
/**
* Class MessageReader
*/
class MessageReader extends RecursiveArrayIterator
{
}
$message = new Message();
$message->title = "Take a look to my garden!";
$message->body = "Take a look and give me your feedback about it! It's amazing! ;)";
$message->addAttachment('pictureOne.png');
$message->addAttachment('pictureTwo.png');
$message->addAttachment('pictureThree.png');
$client = new MessageReader($message);
foreach ($client as $attribute => $value) {
if ($client->hasChildren()) {
$output = 'Messages has attachments:';
foreach ($client->getChildren() as $child) {
$output .= ' '.$child;
}
echo $output;
} else {
echo '<h4>'.$attribute.'</h4>';
echo '<p>'.$value.'</p>';
}
}
// output
/*
title
Take a look to my garden!
body
Take a look and give me your feedback about it! It's amazing! ;)
Messages has attachments: pictureOne.png pictureTwo.png pictureThree.png
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment