Jump to content

How do i make a class properties be saved ? e.g. below...sorry for posting here.


jd2007

Recommended Posts

<?php
// this is hello.php
class Hello
{
protected $text;

function getText($text)
{
   $this->text = $text;
   echo "<b>$this->text</b>";
}

function underline()
{
   echo "<u>$this->text</u>";
}
}

$obj = new Hello();
$obj->getText("hello !");

?>

 

in the above i created a $obj object from Hello's class. I call the getText function. now, after some time, if i call the underline function in the same object like this:

require("hello.php");
$obj->underline();

 

nothing gets displayed because my object doesn't get saved, how do i save the object, so that $obj object remembers the property ($this->text) value ?

jd2007, are you calling the $obj->underline() method from the same page as you set the text? If not, then the object will not be propagated to the next page. There are a number of ways you could solve this, either save the objects properties to a database/text file somehow or serialize the object, save it to a Session variable and the deserialize on the next page.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.