PHP Object to XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kissko86
Forum Newbie
Posts: 3
Joined: Fri Nov 19, 2010 6:43 am

PHP Object to XML

Post by kissko86 »

Hi,

I am currently building a very simple CMS. I am using OOP for generating static pages. However, I am having a problem with storing the objects. Can anyone suggest how can this be done by storing objects into XML files? I have heard of serialization but don't understand that approach completely.

//example of my class

class Page {
private $title;
private $heading;
private $content;

public function Page($t,$h,$c) {
$this->title = $t;
$this->heading = $h;
$this->content = $c;
}

public function getTitle() {
return $this->title;
}

public function getHeading() {
return $this->heading;
}

public function getContent() {
return $this->content;
}
}

//instantiating object

$title = $_POST['title'];
$heading = $_POST['heading'];
$content = $_POST['content'];

$myPage = new Page($title, $heading, $content);

//after this it should be stored as XML file, but don't know how

Thanx in advance.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Object to XML

Post by Jonah Bron »

You can just serialize your Page object, XML is probably unnecessary in this case.

Code: Select all

$myPage = serialize($myPage);
file_put_contents('somefile.txt', $myPage);

// later, to get it back,

$myPage = file_get_contents('somefile.txt');
$myPage = unserialize($myPage);
// voilà!  Your object is back.
You can look at the contents of the text file to see what your serialized object looks like.
kissko86
Forum Newbie
Posts: 3
Joined: Fri Nov 19, 2010 6:43 am

Re: PHP Object to XML

Post by kissko86 »

Thanx..that works perfectly. However, I have another question.
As you suggested, the objects (Pages) will be stored in the text file such as about.txt, faq.txt and so on. How can I then iterate through these files to create navigation dynamically?
I know how it could be delivered by using database, but I don't want to use database for these pages.

to explain by what I mean I will give you the example of the database solution.

$result = sqlite_query($db, "SELECT * FROM Page");

while ($row = sqlite_fetch_array($result, SQLITE_ASSOC)) {
echo "<a href='mysie.php?page=" . $row['title'] . ">" . $row['title'] . "</a>";
}

Any ideas for the OO approach?

Thanx in advance
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP Object to XML

Post by AbraCadaver »

Code: Select all

foreach(glob('pages/*.txt') as $page) {
   $title = str_replace('.txt', '', $page);
   echo "<a href='mysie.php?page=" . $title . ">" . $title . "</a>";
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kissko86
Forum Newbie
Posts: 3
Joined: Fri Nov 19, 2010 6:43 am

Re: PHP Object to XML

Post by kissko86 »

Thanx a lot. That works perfectly. :)
Post Reply