Re: Dear PHP guy
| From: | Dave Mertens | Date: | Sun, 15 Jul 2001 15:04:39 +0000 |
| Subject: | Re: Dear PHP guy | ||
| References: | 1 | Groups: | php.mirrors |
| Request: | Send a blank email to php-mirrors+get-5567@lists.php.net to get a copy of this message | ||
On Sat, Jul 14, 2001 at 11:43:11PM -0700, Majiko wrote:
> But, unfortunately, not even the searches can help me.
> I searched for a forum, but found none.
An good forum to start with is phorum. U can find it on http://www.phorum.org.
It GPL, so you can use it. But, there's a but; Some parts are saved to files
and other parts to database, why not save it all to database..
> So, I have a simple question to ask of you...
>
> How do I save a class with all of it's current
> variables and re-load them successfully? I mean, save
> the class, goto another PHP program, re-load the
> entire thing, just like in the program that saved it?
> No variables changed or anything.
The easiest way to save data from a class is using a database.
A populair database server is MySQL. If you're planning to do a lot of writing actions or want
to use
transactions i advise you to use PostgreSQL.
Let's save you have a class MyData:
class MyData
{
var $name;
var $message;
var $dbconn;
}
This class is now equal to an type definition is VB. But now we're adding some functions for
saving and
loading from database. Because i make use a postgres database, i use the postgres functions, but
most of the MySQL
functions are similair.
First we're going to add an constructor:
function MyData($dbconn)
{
//dbconn is a active database connection
$this->dbconn = $dbconn;
return 1;
}
Notice the special keyword '$this->'. With this you calling the class variables
internaly.
function Load($name)
{
//we're gonna load the class with the data found in the resultset (in windows called
recordset)
$query = "select * from tblMessage where name='" . $name . "'";
$rsid = pg_exec($this->dbconn, $query);
if (pg_numrows($rsid) > 0)
{
//get only the first record
$rs = pg_fetch_object($rsid, 0); //fetch first row as object
$this->name = $rs->name;
$this->message = $rs->message;
return 0; //record fetch successfull
}
else
return 1; //no records found
$pg_freeresult($rsid); //close resultset
unset($rs);
}
And ofcourse we're want to save the data as received from a form post
function Save($name)
{
//class data is filled with form post
$query = "update tblMessage set message='" . $this->message . "' where
name='" . $this->name . "'";
if (pg_cmdtuples(pg_exec($this->dbconn, $query) > 0)
{
//query was successfull
return 0;
}
else
{
//there was an database error
return 1;
}
}
All the function must be place inside the class.
the class is using an table called 'tblMessage' with columns 'name' and
'message'.
On www.zend.com there are a lot of database abstraction layers. If you're farmiliar
with ADO objects from MS, than maybe ADODB for php4 from http://php.weblogs.com.
Here's some code to call and use the class
//loading class
$mc = new MyClass($cnDatabase); //$cnDatabase is an active connection with an PostgreSQL databas
server
$mc->load("php");
echo $mc->message . "<br>";
//saving class data
$mc = new MyClass($cnDatabase);
$mc->message = $HTTP_POST_VARS["message"];
$mc->save($HTTP_POST_VARS["name"];
> I suspect there is a command for it, but I cannot find
> it. Otherwise, I will have to result to reading one
> byte at a time, making the process much more hectic.
The reason why you didn't find a saving/loading function is because you don't know HOW to
save it..
You can save the data to file, database, xml or session. It's up to you to choose the right
one. Don't forget that
internet is stateless. You don't know what the user has done. There most sites give users
cookies to track them.
With the id from the cookie, that user data is loaded again from the database. All your is gone when
is page is finished!
Saving data to file and reading is harder, because you than have to parse the file.
A good resource for php code is http://www.zend.com.
Another advise: keep your first classes simple. If you now have a function with 3 or 4 argument, try
to make a class
of it. It's a good pratice. Later you can extend your classes or use classes within your
classes.
Oh, and don't try to understand it in a few hours. It's will come to you itself.
Good luck,
Dave Mertens