Jump to content

OOP code sample


jimfog
 Share

Recommended Posts

Here is some code:

$now = new DateTime; echo 'startdate='.$startDate = $now->modify("tomorrow")->modify("last Monday")->format('Y n d').'<br>';  echo  'enddate='.$endDate =  $now->modify("yesterday")->modify("+7 days")->format('Y n d').'<br>';

and here is the output: startdate=2012 5 21enddate=2012 5 27 So far nothing strange. Now the strange part-if I erase startDate and just keep the code that corresponds to the endDate-the output I get is this: enddate=2012 5 29.Why the above? Are startDate and endDate "tied somehow" together? I have not fully understand yet OOP concepts.

Link to comment
Share on other sites

Everytime you use $now with modify you are changing this date, that is why you should clone startDate and endDate with $now $now should remain the same date value.

  <?php$now = new DateTime; echo '$now before Startdate modify '.$now->format('Y n d').'<br>'; echo 'startdate='.$startDate = $now->modify("tomorrow")->modify("last Monday")->format('Y n d').'<br>'; echo '$now after Startdate modify '.$now->format('Y n d').'<br>'; echo  'enddate='.$endDate =  $now->modify("yesterday")->modify("+7 days")->format('Y n d').'<br>'; echo '$now after Enddate modify '.$now->format('Y n d').'<br>';echo '<hr /><h3> Now with clone </h3>'; $now = new DateTime; echo '$now before Startdate modify '.$now->format('Y n d').'<br>'; $startDate = clone $now;$endDate = clone $now;echo 'startdate='.$startDate->modify("tomorrow")->modify("last Monday")->format('Y n d').'<br>'; echo '$now after Startdate modify '.$now->format('Y n d').'<br>'; echo  'enddate='.$endDate->modify("yesterday")->modify("+7 days")->format('Y n d').'<br>'; echo '$now after Enddate modify '.$now->format('Y n d').'<br>'; echo $now->format('Y n d').'<br>'; ?>

Edit: to get the correct result with '+7 days' you would need to clone startDate after it is modified $endDate = clone $startDate;

Edited by dsonesuk
Link to comment
Share on other sites

Ok, this is the code(which I think you gave me) which is related with the calendar app(made in another post):

$now = new DateTime($newdate);$today=$now;//get start and end date of specific date value $newdate$startDate = $today->modify("tomorrow")->modify("last Monday")->format('Y m d');$endDate =  $today->modify("yesterday")->modify("next Sunday")->format('Y m d');

Why don't you use clone here? Why?The assumption Ι make is that it is not needed probably.

Link to comment
Share on other sites

Yea I original used a clone for both then I copied some of your code, without cloning and went on wrong from there, but! it should still give the correct end date if you remove Startdate because it is looking at the 'next Sunday' date from $now date. Whereas the '+7 days' is looking for fixed 7 day period forward from Startdate when it is modified to last mondays date, but when you remove startdate modification it will be +7 days from date set in $now. IF you get my drift. now = 23/05/2012startdate = 21/05/2012 using +7 daysenddate ((-1day)+7) with startdate = 27/05/2012enddate without startdate (now(-1day)+7) = 29/05/2012 using next sundayenddate ((-1day) 'next sunday') with startdate = 27/05/2012enddate without startdate (now(-1day) 'next sunday') = 27/05/2012

Link to comment
Share on other sites

Yes I got your drift.Do you think I should now use clone in the code you gave me?It works as you said. But I am just wondering if there is any reason at all to use clone-or just leave it the way it is?

Link to comment
Share on other sites

I think it would be better practice to use clonning, as $now value will remain unchanged and constant so you know that the value used in formulas is the correct value to check against, and has not been modified into a different value that could produce results you did not expect because of this.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...