Jump to content

object, classes and moustaches


wright67uk

Recommended Posts

Im exploring the world of classes, methods and objects and im after some explanation to my code below;

 

1) what does  echo $oTime->sTime;  do in this code.

2) why do i NOT need to add "$sTime =" before "$oTime->ShowFutureDate(20); " for the code to execute properly (as i did in line 8)

 

time.php

<?php
include('class_time.php');  
//include file where class is stored

$oTime = new Time;  
// VARIABLE = new CLASSNAME  $oTime is now the object variable

$sTime = $oTime->GenerateCurrentTime(); 
// use GenerateCurrentTime within the $oTime class 
//  -> means 'is the parent of' $oTime is the parent of ' GenerateCurrentTime()'

print 'The time is: ' . $sTime;   
// print value of GenerateCurrentTime

echo $oTime->sTime;  

echo "<br/>"; 
// new line

$oTime->ShowFutureDate(20);  
print 'The future time is: ' . $sTime; 
echo $oTime->sTime;

?>

 

class_time.php


<?php
class Time  
{         

var $stime;

  function GenerateCurrentTime() { 
       $this->sTime  = gmdate("d-m-Y H:i:s");  //assign the current date to the sTime variable
  } 
  
  function ShowFutureDate($iAddDays=0) {   
     $this->sTime  = gmdate("d-m-Y H:i:s", strtotime("+" . $iAddDays . " days"));      
  }  
} 
?>

Link to comment
Share on other sites

You're confusing yourself because the class code is written in PHP 4 syntax (the var keyword).  That leads to the $sTime member variable being public, and, since you also have an $sTime local variable in play, things are getting muddled.  Use PHP 5 syntax instead:

 

class Time {
   private $sTime;
   
   function GenerateCurrentTime() { 
      $this->sTime  = gmdate("d-m-Y H:i:s");  //assign the current date to the sTime variable
   } 
  
   function ShowFutureDate($iAddDays=0) {   
      $this->sTime  = gmdate("d-m-Y H:i:s", strtotime("+" . $iAddDays . " days"));      
   }  
}

 

That might make things more clear.  Also, get this book: http://www.amazon.com/Objects-Patterns-Practice-Experts-Source/dp/143022925X/ref=sr_1_1?s=books&ie=UTF8&qid=1333840120&sr=1-1

 

Finally, -> does not mean 'parent of'.  I don't know where you got that idea, but it's wrong.  -> is the 'to member' operator, meaning that with $oTime -> GenerateCurrentTime(), you're accessing the member function/method named GenerateCurrentTime of the Time object referenced by the variable $oTime.

Link to comment
Share on other sites

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.