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

Laravel News:
Laravel 5.7 Mail Localization Improvements
Oct 04, 2018 @ 09:18:41

In this new post to the Larvel News site, they cover some of the recent improvements in the mail handling for the framework, specifically in the use of localization to tailor the content for a certain language.

Laravel 5.7.7 is now released and includes both bug fixes and thanks to Derek MacDonald a big improvement to Mail Localization.

With the release of Laravel 5.7 the IlluminateNotificationsNotification class started offering a locale method to set the desired language. The application will change into this locale when the notification is being formatted and then revert back to the previous locale when formatting is complete.

The post provides an example of how to use this functionality in sending your emails and how to add a "locale" field to your models. This allows you to set up a preferred default locale for the data, removing the need to reference it directly every time.

tagged: mail localization improvement laravel tutorial example

Link: https://laravel-news.com/mail-localization

StarTutorial:
Modern PHP Developer - PSR
Oct 02, 2018 @ 13:21:50

If you're new to PHP or are working to enhance your skills, chances are you've at least heard of the PHP-FIG and the several PSRs that it has released to help provide structure around common functionality. In this article from StarTutorial they walk you through some of the basics of the more widely adopted PSRs including PSR-0/PSR-4 and PSR-3/PSR-4.

Prior to PHP Standards Recommendation (PSR), there were no truly uniformed standards for writing PHP code. For instance, for coding style, some people preferred Zend Framework Coding Standard, and some liked PEAR Coding Standards, and still others chose to create their own naming conventions and coding style.

[...] At the time of this writing, there are six accepted PSRs: two of them are about autoloading, two of them are related to PHP coding style and the remaining are about interfaces. In this chapter, we will discuss each PSR briefly. The purpose of this chapter is to introduce you to the ideas of PSRs. For further details on each one, the respective link are provided.

The post then goes through each of the major PSRs, describing them and providing code examples where relevant:

  • PSR-0 & PSR-4 for autoloading
  • PSR-1 & PSR-2 for coding standards
  • PSR-3 for logging interfaces
  • PSR-7 for HTTP message stricture

The post finishes with links to each of the PSRs on the PHP-GIF site for more information.

tagged: psr example psr0 psr4 psr3 psr2 psr1 psr7 tutorial phpfig

Link: https://www.startutorial.com/articles/view/modern-php-developer-psr

CodeWall:
5 Ways To Loop Through An Array In PHP
Sep 25, 2018 @ 11:04:25

On the CodeWall site Dan Englishby walks you through some of the basic functionality in the PHP language for working with arrays. In it he shows how to loop through an array using multiple tools including the usual control structures and others less widely used.

PHP, just like most other programming languages has multiple ways to loop through arrays. The most popular ways to do it usually is with a while, for and foreach operator, but, believe it or not, there more ways to do it with PHP. In this article I will walk-through each possibility for reading arrays whilst looping.

He breaks the article up into sections, one for each of the methods:

  • the white loop
  • the for loop
  • the foreach loop
  • the do/while loop
  • using the ArrayIterator

Each item in the list comes with a summary of how it works and some code showing it in action.

tagged: introduction tutorial language loop array top5 example

Link: https://www.codewall.co.uk/5-ways-to-loop-through-array-php/

Stitcher.io:
Laravel view models
Sep 24, 2018 @ 12:27:50

On the Sticher.io site a new tutorial has been posted introducing you to Laravel view models. This functionality allows you to remove view-only logic from other parts and isolate it for transformation.

View models are an abstraction to simplify controller and model code. View models are responsible for providing data to a view, which would otherwise come directly from the controller or the model. They allow a better separation of concerns, and provide more flexibility for the developer.

In essence, view models are simple classes that take some data, and transform it into something usable for the view.

The post starts with some of the basics behind the "view model" design pattern and jumps in to an example for a blog site. In it, the code pulls in the category listing that's needed to display the page, removing the need for it to be in the controller code. It also includes the addition of custom logic to the model and the refactoring that can help move the logic into it. The tutorial also includes a section covering some of the "niceties" that can be added including passing it directly to the view method, returning it as JSON and returning individual properties as JSON.

tagged: laravel tutorial viewmodel designpattern example introduction

Link: https://stitcher.io/blog/laravel-view-models

Matthias Noback:
Assertions and assertion libraries
Sep 21, 2018 @ 10:52:35

In a new post to his site Matthias Noback takes a look at the concept of assertions and some libraries including some effective ways to use them in your code for validation of values.

When you're looking at a function (an actual function or a method), you can usually identify several blocks of code in there. There are pre-conditions, there's the function body, and there may be post-conditions. The pre-conditions are there to verify that the function can safely proceed to do its real job. Post-conditions may be there to verify that you're going to give something back to the caller that will make sense to them.

[...] Sometimes the programming language itself can help with these pre-conditions: for instance, the language may support strict typing, which prevents certain types of invalid input to be provided. Some languages offer more advanced ways of defining pre-conditions, like pattern matching.

Following a brief use case for assertions (at a high level) he gets more specific to PHP and mentions two assertions libraries that could be used to add these kinds of checks to your code (in addition to PHP's own assert function). He then answers the "why use assertions?" question and some basic rules around using them:

  • don't use assertions to validate user input, use it to validate function arguments.
  • don't use assertions to validate return values from other functions.
  • don't use assertions as a replacement for exceptions.

For each of these, he provides a summary with a bit more background and code examples to help illustrate the point. He ends the post with some useful "rules of thumb" when using assertions and a reminder:

Assertions are sanity checks. When they would be left out, you should still have a correctly function application. They should never become user-facing errors.
tagged: assertion library tutorial example suggestion

Link: https://matthiasnoback.nl/2018/09/assertions-and-assertion-libraries/

php[architect]:
PHP 7.3 is On Track
Sep 17, 2018 @ 11:19:58

On the php[architect] site they've shared another article from their September 2018 issue. In this new article author Damien Seguy looks forward to PHP 7.3 and covers some of the new features that come with it.

PHP 7.3 successfully passed the “feature freeze” deadline. On Aug. 1st, 2018 all features for PHP 7.3 were identified. This triggered the first PHP 7.3 beta, on the following day, and, from there, we’ll reach RC in September. It is time to review what this new PHP version has available for us, help test PHP 7.3, and get ready.

He goes through several of the changes coming including:

  • Improved Garbage Collector
  • Relaxed Heredoc/Nowdoc
  • Trailing Comma for Calls
  • Deprecated Case-insensitive Constants
  • PCRE 2.0
  • SQLite 3.24
  • Json_encode May Throw Exceptions
  • array_first_key(), array_last_key()
  • list() with References
  • is_countable()
  • net_get_interfaces()
  • Removing image2wbmp()
  • assert() is Now a Reserved Function
  • Continue for Loops, Break for Switch
  • Monotonic Timer: hrtime()
  • compact() Reports Undefined Variables

For each of the items listed, there's a brief explanation of what the feature is and some code to show it in action. The tutorial ends with a listing of some things you can do to prepare your current codebase to work with PHP 7.3 ahead of the December 13th release date.

tagged: language release php73 lookahead preparation features tutorial example

Link: https://www.phparch.com/2018/09/php-7-3-is-on-track/

Liam Hammett:
Bitmask Constant Arguments in PHP
Sep 12, 2018 @ 10:32:33

On his Medium.com blog Liam Hammett has written up a tutorial explaining the functionality and use of bitmask constant arguments in PHP.

PHP has a handful of core functions that can accept boolean arguments in the form of constants that have a binary value.

These can be combined together in a single function argument, essentially passing multiple boolean flags in a very compact manner. They work a bit differently to how most people implement options in their userland functions, so let’s take a look at how they work.

He starts off by talking about how the PHP core language makes use of them in certain functions with an example of the JSON_THROW_ON_ERROR constant for use with json_encode (both as a single option and multiple using a bitwise operator). He then gets into the "code behind the code" and talk about how they work for both "OR" and "AND" types. He ends the post with an example putting all of this knowledge to use in an if that detects if a bit exists in the inputted constant list.

tagged: bitmask constant argument tutorial example introduction

Link: https://medium.com/@liamhammett/bitmask-constant-arguments-in-php-cf32bf35c73

Cees-Jan Kiewiet:
React/cache in use
Sep 10, 2018 @ 12:55:11

In a new post to his site Cees-Jan Kiewiet shares details about the latest version of the React/Cache component for the ReactPHP library. In the tutorial he covers the library, what this update brings with it and how he's making use of it.

Recently we, ReactPHP, released 0.5 of our cache package with TTL and other PSR-16 similarities. In this post we'll go over which packages I recently updated and how I am using them in my sites.

He breaks the post up into a few different sections showing the caching in use:

  • JSON and msgpack
  • Redis
  • Fallback
  • react/http session middleware
  • react/http webroot preload middleware

For each item in the list there's a bit of code showing it in action and some explanation as to what it's doing and how it helps.

tagged: reactphp reactcache example redis fallback middleware json msgpack

Link: https://blog.wyrihaximus.net/2018/09/react-cache-0-5/

Pineco.de:
Examples, Tools and Resources for Regular Expressions
Aug 01, 2018 @ 11:27:05

On the Pineco.de blog there's a tutorial posted introducing regular expressions and providing some examples and links to external resources/tools to help put them to use in your code.

Using Regular Expressions is not easy. Mostly we have the feeling we need to learn a new language on the top of those we already know. But, the power and the flexibility that RegEx provides, make it worthy to learn. Take a look at some useful patterns, tools, and sources!

The examples they provide show the matching all the words and matching all the content between specified tags. They end the post linking to several helpful tools including the Laracasts regular expression video and the regexr.com testing tool.

tagged: regularexpression tool resources tutorial introduction example

Link: https://pineco.de/examples-tools-and-resources-for-regular-expressions/

Exakat Blog:
5 usages of static keyword in PHP
Jun 20, 2018 @ 10:28:21

On the Exakat blog there's a new post sharing five uses of the "static" keyword in PHP applications. This includes the less common static closures and static class names.

Static is a PHP keyword with many usages. It is almost universally used, though there are many variations of it. Let’s review all the five of them :
  • static method
  • static property
  • static closure
  • static variable
  • static as a classname

The post goes through each of the items in the list giving a brief explanation of how it's used and a code example showing it in action. The post finishes with some tips on evaluating your own code for the use of "static" and tips for each to make your code easier to wrangle and maintain in the future.

tagged: static keyword example tutorial method property closure variable classname

Link: https://www.exakat.io/5-usages-of-static-keyword-in-php/