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.
PHP Object to XML
Moderator: General Moderators
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: PHP Object to XML
You can just serialize your Page object, XML is probably unnecessary in this case.
You can look at the contents of the text file to see what your serialized object looks like.
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.Re: PHP Object to XML
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
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
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: PHP Object to XML
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.
Re: PHP Object to XML
Thanx a lot. That works perfectly. 