Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Laravel News:
Working with Mutable and Immutable DateTime in PHP
May 30, 2018 @ 14:34:51

On the Laravel News site there's a tutorial posted showing how to work effectively with mutable and immutable DateTime functionality in PHP. The DateTime functionality has long been bundled with the language and offers powerful tools for working with dates, improving on what the basic date function provides.

Mutable dates can be the source of confusion and unexpected bugs in your code. My goal isn’t to tell you that DateTime is evil because it’s mutable, but to consider the tradeoffs and benefits of using mutable versus immutable DateTime objects. Either approach warrants a good test suite and an awareness of how modifier methods affect your date objects.

Until recently, I wasn’t even aware that PHP offers a counterpart to the DateTime class: DateTimeImmutable. The DateTimeIummtable class works just like the DateTime class, except that it never modifies itself, but returns a new object instead. So if you know how to work with DateTime, you immediately can work with DateTimeImmutable.

The article starts by talking about mutable DateTime objects and shows examples of using the Carbon date handling package as a layer on top of PHP's DateTime handling. It includes code examples of mutable objects. It then moves on to the immutable objects, covering the differences between the two complete with code examples. The post ends with a bit more information about mutable vs immutable and links to the Chronos documentation for more information on another date handling library.

tagged: datetime tutorial carbon package mutable immutable

Link: https://laravel-news.com/mutable-and-immutable-date-time-php

MyBuilder Tech Blog:
Insertion, Removal and Inversion Operations on Binary (Search) Trees in PHP
Jul 22, 2015 @ 15:08:21

The MyBuilder.com Tech blog has a tutorial posted showing you how to work with binary trees in PHP, specifically how to perform insertion, removal and inversion operations on their data.

Recently Max Howell (creator of Homebrew) posted an interesting tweet in regard to Google's interview process. In this tweet he mentioned how one of the proposed questions was to white-board a solution to invert a binary tree. Over the past couple of years I have been interested in exploring fundamental Computer Science data-structures and algorithms. As a result, I thought it would be interesting to explore this structure and associated operations in more depth - using immutable and mutable PHP implementations to clearly highlight the benefits garnered from each approach.

He starts with a brief definition of what a binary search tree is just to be sure everyone is on the same page. He then gets into the code to represent a Node, a simple class that has a value and "left" and "right" variables to contain each of the possible two child nodes. He then goes through each of the operations (insertion, removal and inversion) showing code examples for both mutable and immutable methods.

tagged: binarytree insertion removal inversion mutable immutable

Link: http://tech.mybuilder.com/insertion-removal-and-inversion-operations-on-binary-search-trees-in-php/

Nicolas Bérard-Nault's Blog:
The unknown value of Value Objects
Mar 31, 2011 @ 17:12:46

Nicolas Bérard-Nault has put together a new post looking at the role that Value Objects play in application development and, more specifically, how they fit in with domain driven design (DDD). He looks to explain the unknown value of Value Objects to developers that might not know how helpful they really can be.

One of the main rules of DDD is that value objects are immutable. This is often not as self-evident as it seems, as many programmers are not even aware of the state they leak and create. Sadly, state is often positively correlated with entropy. Hence, taking steps to contain and to limit state is one of the keys to taming complexity in an application.

He notes that even outside of DDD Value Objects can be quite useful. He gives an example of a "RationalNumber" class with methods for basic things like addition and subtraction. He shows the more traditional mutable version that most developers would start with and uses it in several examples to show its flaws. He finishes up the post with a look at the "more correct" immutable version of the class and a sample call that would result in the correct output of a simple matematical operation.

tagged: valueobject domaindrivendesign mutable immutable

Link:


Trending Topics: