Hi, I have searched the internet for help on this.
I have a class which has many member variables which are inherently strings like
class something
var Firstname='';
var address=" ";
var phone"";
var todays_date" ";
var numberofchildren =0;
etc etc
Then I want to put these out in other php scripts
so it seems Ihave to use __tostring() in the class somewhere BUT
all the examples I can find have just one variable and then illustrate __tostring written for the output of that one variable
I know I can use a cast to a string in some way
But I am tearing my hair out and i don't have much to spare.
So If any kind person will point me in the right direction I shall be very grateful !!
--
Dave
novice very confused about tostring
Moderator: General Moderators
Re: novice very confused about tostring
Post the whole class, and what you're trying to do please. Use code tags...
Re: novice very confused about tostring
Using "var" is deprecated in favour of public/private/protected. If you set your variables to public, then you can access them from outside the class using:
Code: Select all
$string_value = $YourClass->variable;Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: novice very confused about tostring
Thanks folks that is some help.
I am used to Delphi OOP for some 10years. there all variables in a class are grouped. those under a heading 'private' are private thiose under the group protected are and those under the public are public. Everything in a class is assumed a member of the class and then there is no need for all the 'this' business.
Fred.hat is a hat of the fred class.
However my book says that 'var' although deprecated = public (as from earlier php) and that I must use 'tostring' to output strings...which I find strange.
Of course Delphi is closely typed so a variable is declared at the outset as a string and always remains so. So does not need a cast.
Here if you are interested is the code for class 'tempbook' which is a temporary booking. (in my work all classes start with capital T
Then in another script I put at some point
I tried to force the types by initialising with a string but PHP won't have that.
So thanks I will try your solution but I am still confused about 'tostring' except as applied to a class as a whole rather than its member variables
--
Dave
I am used to Delphi OOP for some 10years. there all variables in a class are grouped. those under a heading 'private' are private thiose under the group protected are and those under the public are public. Everything in a class is assumed a member of the class and then there is no need for all the 'this' business.
Fred.hat is a hat of the fred class.
However my book says that 'var' although deprecated = public (as from earlier php) and that I must use 'tostring' to output strings...which I find strange.
Of course Delphi is closely typed so a variable is declared at the outset as a string and always remains so. So does not need a cast.
Here if you are interested is the code for class 'tempbook' which is a temporary booking. (in my work all classes start with capital T
Code: Select all
class Ttempbook
{
var $tempbooknum=0;
var $daterow;
var $userlist = array();
var $userindex = 0;
var $length = 0;
var $ahead = 0;
var $letter= 'z';
var $email = '';
var $startdate='abc ';
var $enddate=' ';
var $startday='';
var $endday = '';
function __construct($tempbooknum,$daterow)
{
$this->tempbooknum = $tempbooknum;
$this->daterow = $daterow;
$this->startdate = $daterow['startdate'];
$this->enddate = $daterow['enddate'];
$this->email = $daterow['email1'];
$startbits = explode('/',$this->startdate); // dd/mm/yyyy
$endbits = explode('/',$this->enddate);
$starttime = mktime(0,0,0,$startbits[1],$startbits[0],$startbits[2]);
$this->startday = date("D",$starttime); //day of week
$endtime = mktime(0,0,0,$endbits[1],$endbits[0],$endbits[2]);
$this->endday = date("D",$endtime);
$uksdate = $startday." ".$startdate;
$ukedate = $endday." ".$enddate;
$aday = (60 * 60 * 24);
$this->length =($endtime-$starttime )/$aday +1;
$today = time();
$this->ahead = (mktime(0,0,0,$startbits[1],$startbits[0],$startbits[2]) - $today)/$aday;
$db = & JFactory::getDBO();
$qry =
"select " .$db->nameQuote('periodletter').
"," .$db->nameQuote('periodend').
"from " .$db->nameQuote('#__1_periods').
";";
$db->setQuery($qry);
$letters=$db->loadResultArray(0);
$days = $db->loadResultArray(1);
$len=$this->length;
// echo "<p>days = "; print_r($days); echo"</p>";
for($jj=0;$jj<5;$jj++){
if ($days[$jj] > $len)
break;
}
$this->letter=$letters[$jj];
} // end of construct
function addUser($username)
{
$this->userlist[]=$username;
}
}// end of class so far
Code: Select all
$thebooking = new Ttempbook($tempbooknum,$daterow);
.
.
echo "<p>$thebooking->$email</p>";
//and various other echo ...<p>$thebooking->variables</p> with the output format controlled by css
That gives an error message
So thanks I will try your solution but I am still confused about 'tostring' except as applied to a class as a whole rather than its member variables
--
Dave
Re: novice very confused about tostring
I can tell you that $thebooking->$email will not return anything, however $thebooking->email should return the email var inside that class assuming the variable isnt protected or private.daffy wrote:Code: Select all
$thebooking = new Ttempbook($tempbooknum,$daterow); . . echo "<p>$thebooking->$email</p>"; //and various other echo ...<p>$thebooking->variables</p> with the output format controlled by css That gives an error message
Your other option, for testing of course, is it simply create a 'get' method for the var.
Code: Select all
function getEmail(){
return $this->email;
}
Re: novice very confused about tostring
Perhaps you misinterpreted it, but that's not good advice at all.However my book says that 'var' although deprecated = public (as from earlier php) and that I must use 'tostring' to output strings...which I find strange.
__toString() is syntatic sugar only, its not a necessity. It's used to allow entire objects to be cast into string, but you can definitely output members and method results without it.