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

Tomas Votruba:
Function create_function() is Deprecated in PHP 7.2 - How to Migrate?
Dec 18, 2018 @ 15:34:16

In this post to his site Tomas Votruba covers the create_function function, its deprecation in PHP 7.2 and how to refactor your code to remove it.

If there would be "Miss Deprecation of PHP 7.2", create_function() would definitely win. They can be very complex, tricky and very hard convert to PHP code. Moreover without tests.

Do you have over 5 create_function() pieces in your code? Let's see how to migrate them.

He starts by talking about why it's being deprecated (it's essentially an eval) and some examples of how it could be used to execute code. He then goes through several usage examples and shows how to refactor them into anonymous functions. In his examples he uses a mix of regular code conversions and a sort of hybrid using strings and code to replace the string (probably) used previously with create_function.

tagged: createfunction function deprecation anonymous tutorial refactor

Link: https://www.tomasvotruba.cz/blog/2018/12/17/function-create-function-is-deprecated-in-php-72-how-to-migrate/

Laravel News:
Unwrapping array_wrap()
Nov 30, 2018 @ 15:34:32

On the Laravel News site they have a new post that "unwraps" the array_wrap helper that makes it easier to normalize values into arrays and removes the need for manually checking if a variable is an array or set before using it like one.

Laravel has a wrap method and array_wrap() helper to normalize values into an array. Raul @rcubitto shared this nice tip about it on Twitter and before seeing his Tweet I wasn’t aware of this method.

They include an example of the helper function in use and share some of the responses to his tweet including questions of how it's different than casting to an array. They answer this by talking about more complex values and how the helper is useful for null values and creating consistency in your application.

tagged: laravel helper function array arraywrap tutorial

Link: https://laravel-news.com/array_wrap

TutsPlus.com:
Understand Arrays in PHP
Sep 05, 2018 @ 15:48:04

In a recently posted tutorial on the TutsPlus.com site, author Sajal Soni gets back to the basics and covers the use of arrays in PHP.

In this post, you'll learn the basics of arrays in PHP. You'll learn how to create an array and how to use associative and multidimensional arrays, and you'll see lots of examples of arrays in action.

The tutorial then starts with a brief introduction to what arrays are and how they're defined. From there it shows:

  • how to access array elements
  • types of arrays
  • some useful array-related functions

Each item (and function example) comes with a code snippet to show it in use as well as a brief summary of what's happening.

tagged: array introduction basics tutorial function types

Link: https://code.tutsplus.com/tutorials/understand-arrays-in-php--cms-31738

Laravel News:
New Outer Array Functions Coming to PHP 7.3
Jul 18, 2018 @ 17:47:44

On the Laravel News site they've shown a spotlight on a new feature that's coming with the next major release of the PHP language (v7.3): outer array functions.

PHP 7.3 introduces two new array functions for working with the “outer” keys of an array. The RFC proposal included four new functions for both keys and values, but only the array key functions were accepted: array_key_first() and array_key_last().

[...] Although the outer array value functions were declined, at least new functions will be available for getting the outer keys of an array.

They then provide some code examples of this new functionality, showing how use these new functions to extract values. It also includes examples of the two functions - array_value_first and array_value_last - that were rejected from the RFC when voting happened.

tagged: array outer function php73 feature key value

Link: https://laravel-news.com/outer-array-functions-php-7-3

Laravel News:
PHP 7.3: Trailing Commas in Function Calls
Jun 15, 2018 @ 15:46:51

In a post in the Laravel News site, they quickly cover one of the many changes coming with the next jump in versions of the PHP language (v7.3): trailing commas in function calls.

Well PHP 7.3 won’t have arrow functions (that would be dreamy). However, trailing commas in function calls is an excellent addition coming to PHP 7.3.

In PHP 7.3, trailing commas in function calls will be valid syntax. That is to say, you can use trailing commas when calling functions, but not defining them.

They then include two places where these trailing commas could be useful: in the use of variadic functions and in PHP array definitions. The remainder of the post shows the concept of "trailing commas" in other languages including Javascript, Python, and Haskell. The first alpha release of PHP 7.3 has been released if you'd like to try this out with your own code.

tagged: trailing comma function call php73 feature alpha tutorial

Link: https://laravel-news.com/php-trailing-commas-functions

Exakat Blog:
The 100 PHP functions that you have to know
May 31, 2018 @ 14:54:33

On the Exakat blog they have a new post with the results from their evaluation of a wide range of PHP applications showing the Top 100 PHP functions in use.

Here is the top 100 PHP functions : it is the list of the most often used PHP native functions.

The functions are named, and ranked from 1 to 100. The other 4500 functions are not ranked now. The frequency column represents how often this function is used across PHP code : the reference corpus is a list of 1900 PHP open source projects. They were audited with Exakat static analysis engine, version 1.2.5. The average is the number of time a function is called within one project. Some function are used in isolation, and others are a staple.

The top 5 on the list are:

Each item on the list comes with "frequency" and "average" scores (that mostly relate directly). Check out the post for the full list and a list of some insights they worked up as they generated the listing.

tagged: top100 list function language ranking usage

Link: https://www.exakat.io/top-100-php-functions/

Exakat Blog:
How many parameters is too many?
May 01, 2018 @ 16:55:47

In a new post to the Exakat blog they try to answer the question "how many parameters is too many" when it comes to the structure of the methods and functions in your application.

Now, that is a classic question, that is often a minefield for anyone writing an increasing long list of argument in a method, or simply trying to set up auditing tools.

Obviously, the answer is not immediate. Parameters may be needed, but on the other hands, currying functions allows to reduce the amount of parameter to one for every function. In between, probably exists a reasonable level that is a golden rule, and also very elusive. So, we decided to check the current practice in PHP code.

They started the research with some of PHP's own native functions that took in specific arguments, ignoring those that took an arbitrary number. Next they made a survey of 1900 open source projects to determine the common practice for parameters by function. The results showed that methods without at least one parameter were "less useful" and that a seemingly reasonable amount of parameters is 5. The post finishes with a spotlight of two they found during their research that had the most parameters: a generated class for database interaction and a dependency injection class.

tagged: parameters count statistics userland native function method results

Link: https://www.exakat.io/how-many-parameters-is-too-many/

Tomas Vortuba:
How I Got into Static Trap and Made Fool of Myself
Apr 30, 2018 @ 15:38:38

Tomas Vortuba has a new post to his site sharing some of his experiences with static function analysis (and fixing) and the trap he got himself into when evaluating the current state of his code. This is a continuation of his series covering the use of the SymfonyCodingStandard package.

PHP story with code examples, copy-cat killers, just a little bit of static, consistency, sniffs and way to prevent all that ever happening ever again.

In his post he uses an example "fixer" for testing DocBlock comments to see if they provide much useful information over what the PHP itself provides. He dives "under the surface" and looks at the steps it takes to accomplish this. He then gets to the "trap" part, showing the code behind the fixer and how depending on 3rd party packages and their parsing lead to code mistakenly being "fixed". He finishes up the post with a look at a few solutions that could help make these "traps" easier to fix including dependency injection and preventing the issue specifically with static functions.

tagged: static function analysis fixer codingstandard tutorial

Link: https://www.tomasvotruba.cz/blog/2018/04/26/how-i-got-into-static-trap-and-made-fool-of-myself/

Pineco.de:
Little Snippets to Keep Your Code Cleaner
Mar 01, 2018 @ 15:45:25

The Pineco.de blog has a post sharing some little snippets of code that can help to keep things cleaner and perform some common operations.

Sometimes it’s harder to keep your code clean and readable than to implement some architecture in your application. We collected some snippets that may help you to refactor your code.

Their list includes code to help with:

  • array casting
  • type checking
  • removing unnecessary "if" statements

They also have several others for different languages on the snippets page of their site for Javascript, Laravel, WordPress and even an .htaccess configuration.

tagged: cleaner code snippet function array typecheck refactor tutorial

Link: https://pineco.de/little-snippets-keep-code-cleaner/

Rob Allen:
Getting started with Serverless PHP
Aug 24, 2017 @ 14:19:19

In a new post to his site Rob Allen talks about getting up and running with "serverless PHP" using the Apache OpenWhisk cloud platform that executes as events are fired rather than sitting as an entire application.

I've been interested in Apache OpenWhisk for a little while now and recently submitted a new feature to add PHP support to the project. As OpenWhisk is a serverless environment, most users do not run their own copy and instead use a commercial provider with IBMs Bluemix available now along with Adobes I/O Runtime and RedHat coming soon. As a result, my contribution, isn't practically useful until it's in production with a provider.

Fortunately, and remarkably quickly, IBM have added support for PHP to the Bluemix "IBM Cloud Functions" platform, so now we can use PHP to develop serverless applications and deploy them into the wild! This is a rebranding, so you'll see this referred to as "Bluemix OpenWhisk" around the web too.

He walks you through the setup of an account on the bluemix.net service and how to install OpenWhisk locally rather than the IBM "Bluemix CLI" tool. He then shares the code to create a simple "Hello World" function in PHP (7.1) and push it up to your account. He then executes the action from the command line, returning his JSON formatted greeting. Next he shows how to set this function up as a web action and give it a URL for the outside world to access. He finishes the post with some examples of calling the URL and how it would receive parameters via a POST or a GET.

tagged: serverless development ibm bluemix function web cli tutorial

Link: https://akrabat.com/getting-started-with-serverless-php/


Trending Topics: