laravel - PHP object is being stored in variable as a reference rather than value -


i have object stores current date using carbon such:

class events {    public $monthrange = 12;    public $today;    public function __construct() {     $this->today = carbon::now();   } } 

i have class extends class want set variable of $d = $this-today such:

namespace events;  use carbon\carbon;  class eventsdata extends events {    public $monthsnames = [     "1" => "january",     "2" => "february",     "3" => "march",     "4" => "april",     "5" => "may",     "6" => "june",     "7" => "july",     "8" => "august",     "9" => "september",     "10" => "october",     "11" => "november",     "12" => "december"   ];   public function next_12_months() {     $next_12_months = [];     $d = $this->today;      array_push($next_12_months, $d->month);     ( $i = 0; $i <= ($this->monthrange - 1); $i++ ) {       $d = $d->addmonths(1);       if ( $this->today != $d) {         array_push($next_12_months, $d->year);       }       var_dump($$this->today); //is being modified along $d       var_dump($d);       $next_month = $d->month;       array_push($next_12_months, $next_month);     }      return $next_12_months;   } } 

the problem when modified $d $d->addmonths(1), seems $this->today gets modified.

how prevent happening?

clone object

$d = clone $this->today; 

more cloning here.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -