jd2007 Posted October 31, 2007 Share Posted October 31, 2007 <?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 ? Link to comment https://forums.phpfreaks.com/topic/75480-how-do-i-make-a-class-properties-be-saved-eg-belowsorry-for-posting-here/ Share on other sites More sharing options...
rajivgonsalves Posted October 31, 2007 Share Posted October 31, 2007 please change protected $text; to private $text; Link to comment https://forums.phpfreaks.com/topic/75480-how-do-i-make-a-class-properties-be-saved-eg-belowsorry-for-posting-here/#findComment-381808 Share on other sites More sharing options...
jd2007 Posted October 31, 2007 Author Share Posted October 31, 2007 why ? Link to comment https://forums.phpfreaks.com/topic/75480-how-do-i-make-a-class-properties-be-saved-eg-belowsorry-for-posting-here/#findComment-381811 Share on other sites More sharing options...
rajivgonsalves Posted October 31, 2007 Share Posted October 31, 2007 sorry that was a wrong solution... my oops concepts are a little weak Link to comment https://forums.phpfreaks.com/topic/75480-how-do-i-make-a-class-properties-be-saved-eg-belowsorry-for-posting-here/#findComment-381812 Share on other sites More sharing options...
Zane Posted October 31, 2007 Share Posted October 31, 2007 do you get any errors if you don't try putting error_reporting(E_ALL); at the very top of your script Link to comment https://forums.phpfreaks.com/topic/75480-how-do-i-make-a-class-properties-be-saved-eg-belowsorry-for-posting-here/#findComment-381814 Share on other sites More sharing options...
pollofrito Posted October 31, 2007 Share Posted October 31, 2007 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. Link to comment https://forums.phpfreaks.com/topic/75480-how-do-i-make-a-class-properties-be-saved-eg-belowsorry-for-posting-here/#findComment-381816 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.