M
M
Msim2015-12-10 23:23:42
PHP
Msim, 2015-12-10 23:23:42

How to change an object instead of returning a new one every time in php?

//Пример возврата нового обьека
$formatString = 'l, F j, Y';
$date1 = new DateTimeImmutable('12/25/2015');
// всегда вернет новый обьект а не будет при modify изменяють себя же
$date2 = $date1->modify('+1 month');
echo 'Date 1: ' . $date1->format($formatString) . '<br>';
echo 'Date 2: ' . $date2->format($formatString) . '<br>';

//Сохранение контекта 
$formatString = 'l, F j, Y';
$date1 = new DateTime('7/4/2015');
$date2 = $date1->modify('+1 month');
// в таком случае даты будут одинаковы т.к. как меняет $date1 и записывает тоже в $date2
// поэтому буду одинаковыми при
var_dump($date1 === $date2);

Question: how to implement the same in php naturally on the oop side

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2015-12-10
@Msim

This class behaves the same as DateTime except it never modifies itself but returns a new object instead.
RTFM .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question