novice very confused about tostring

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
daffy
Forum Newbie
Posts: 7
Joined: Wed Aug 19, 2009 7:29 am

novice very confused about tostring

Post by daffy »

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
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: novice very confused about tostring

Post by Mirge »

Post the whole class, and what you're trying to do please. Use code tags...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: novice very confused about tostring

Post by pickle »

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.
daffy
Forum Newbie
Posts: 7
Joined: Wed Aug 19, 2009 7:29 am

Re: novice very confused about tostring

Post by daffy »

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

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
 
Then in another script I put at some point

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
 
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
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: novice very confused about tostring

Post by Weiry »

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
 
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.
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;
}
 
Also you mentioned that "<p>$thebooking->variables</p>" returned an error, what exactly is the error? (assuming that by 'variables' you are referring to the variables which have been set)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: novice very confused about tostring

Post by Eran »

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.
Perhaps you misinterpreted it, but that's not good advice at all.
__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.
Post Reply