Jump to content

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


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.