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

Rob Allen:
Serverless PHP on AWS Lambda
Jan 03, 2019 @ 16:36:31

Rob Allen has a tutorial posted to his site showing how you can run PHP using the serverless lambda functionality that Amazon Web Services provides.

There are other serverless providers, and AWS Lambda is the market leader, but until recently PHP support could most charitably described as cumbersome. That all changed at the end of 2018 with Lambda’s new runtime API and support for layers.

Let’s look at the practicalities of serverless PHP on Lambda with Serverless Framework.

If you'd like to skip to the "good parts" you can check out this repository of the resulting code. Otherwise, he provides a complete walkthrough of the setup and code required to get the lambda up and running:

  • compiling the PHP binary on an EC2 instance (so it will be compatible)
  • creating a bootstrap file for handling requests
  • setting up the yml configuration for the Serverless framework
  • writing the "hello world" function
  • deploying to the lambda system

Finally he shows how to call the "hello" function using the command line and the response you should receive.

tagged: aws lambda tutorial helloworld serverless framework

Link: https://akrabat.com/serverless-php-on-aws-lambda/

Matthieu Napoli:
Serverless Laravel
Jun 05, 2018 @ 16:53:36

In a new post to his site Matthieu Napoli shows how, with the help of the Bref package, to run a serverless Laravel instance. This is a continuation from his previous post covering the use of serverless functionality for running plain PHP.

Serverless basically means “Running apps without worrying about servers”. The main difference with a traditional hosting is that you do not maintain the servers and reserve their capacity. They are scaled up or down automatically and you pay only for what you use.

[...] Serverless hosting has the advantages of scaling very well since there are (theoretically) no limits. It can also help optimize costs by avoiding paying for unused server resources. You can read more about advantages and drawbacks here.

After briefly introducing what "serverless" computing is, he dives right into the creation of the first lambda on AWS. He then goes through the process of creating the new Laravel project, installing and configuring Bref, and wrapping the Laravel functionality in its HTTP handler. Next, he shares an example of the serverless.yml configuration, how to set up the file storage (for things like sessions and cache) and configure the application to run correctly on the lambda. The post finishes out with a look at setting up the logging, session handling and routing before finally deploying the app using Bref's own command.

tagged: laravel serverless tutorial bref framework application lambda aws

Link: http://mnapoli.fr/serverless-laravel/

Andrew Podner:
Closures, Lambdas, and Anonymous Functions
Feb 21, 2013 @ 19:02:13

Andrew Podner has posted an introduction to a concept in PHP that may not be easy to immediately grasp for someone relatively new to the language - the idea of lambdas and closures.

Beginning with PHP 5.3, the anonymous function, became available to developers as a means of improving flexibility and increasing the number of tools at our fingertips to build powerful PHP applications. What I have been seeing a lot of as I read through documentation is more frequent use of terms like lambda, closure, or anonymous function, and sometime the terms are often used interchangeably, so let’s take a deeper look into what all of these things are and what differences, if any, there are between them.

The post looks at lambdas first, defining them as "functions without names" that can be assigned to objects or called inline when needed. Next is closures, and it's noted that, in PHP, there's really not much difference. They both also allow for use of the "use" statement to import values into the function's local scope. He mentions the "$this" update that came in PHP 5.4 and includes some arguments against their use too.

tagged: closure lambda anonymous function introduction

Link:

Shameer's Blog:
PHP 5.3 : Practical look into Lambda functions and closures
Dec 30, 2010 @ 17:11:26

For those out there still coming to grips with some of what PHP 5.3 has to offer, I'd suggest checking out this new article from Shameer about using lambdas and closures along with some examples putting them into action.

PHP 5.3 packaged the power of functional programming by adding support for lambda functions and closures. You will be familiar with them in javascript. In this article we will have a look into these features and its usages.

He starts with explanations of what they are with simple code examples showing their use - a lambda assigned to a variable and closures with the "use" keyword. He shows how they can be used in application prototyping, making handy callback functions and includes a practical example of finding the factorial of a number using a lambda.

tagged: lambda closure tutorial practical example

Link:

SaniSoft Blog:
Lambda functions and Closures in PHP 5.3c
Oct 12, 2010 @ 15:59:38

On the SaniSoft blog there's a recent post looking at one of the more recent and more powerful features to be included in PHP (as of 5.3) - lambdas and closures.

PHP 5.3 has introduced a whole new range of cool features like Namespaces, late static binding, new error levels, to name a few. But the functionality that I found most handy for day to day programming is the Lambda functions and Closures that found their way in PHP through version 5.3

The introduce the concept of the lambda/closure functions and how they can be used instead of several of the usual callback functions. In their example, they give an array of data (associative inside of a numeric key) and an array_filter call with a closure callback to check the value of the "score" value in each. Their closure example assigns the function to a variable to return the score information from the same dataset.

tagged: closure lambda tutorial function

Link:

Sameer Borate's Blog:
Anonymous functions in PHP
Jun 07, 2010 @ 17:42:33

On his blog today Sameer Borate has a new post talking about anonymous functions (closures, lambdas) in PHP and includes plenty of examples of how to use them.

Anonymous functions are common in various modern languages, Ruby and Javascript being the popular one. But until version 5.3 PHP lacked true anonymous functions. Although newbie programmers are hard-pressed to find a suitable application for anonymous functions, they are indispensable if you do a lot of OOP, and can provide some elegant solutions to some particular problems.

He starts with a look at variable functions both in procedural code an object-oriented then moves to the anonymous/lambda function examples (with some nexting involved) and a few uses for closures.

tagged: anonymous function closure lambda tutorial

Link:

Eli White's Blog:
An intriguing use of lambda functions
Mar 11, 2010 @ 14:48:16

In a new post to his blog today Eli White takes a look at an interesting use of lambda functions he's figured out for a his development at work.

I found a very specific use out of the blue of Lambda functions that I have now used and I see as a great use-case. Which is specifically passing functions/logic from your Controller to your View. In the case of Goodsie, I’m using PHP for my templating language and as usual I’m trying to remove as much logic from my View as possible, while still allowing the view to be malleable.

His method centered around a pagination subview that he wanted to make flexible enough to work with both the standard page view and with an ajax request. Lambda functions came to the rescue by dynamically creating a function based on the request need.

tagged: lambda function pagination subview tutorial

Link:

Recess Blog:
Functional PHP 5.3 Part I - What are Anonymous Functions and Closures?
Aug 19, 2009 @ 16:56:50

Those still trying to get a handle on anonymous functions, lambdas and closures in the recently released 5.3 version of PHP might want to take a look at this new tutorial from the Recess blog. It's the first part of their "Functional PHP 5.3" series.

One of the most exciting features of PHP 5.3 is the first-class support for anonymous functions. You may have heard them referred to as closures or lambdas as well. There's a lot of meaning behind these terms so let's straighten it all out.

They explain the differences between closures and lambda functions (hint: not much) and give code examples for both them and closures.

tagged: anonymous function closure lambda

Link:

Dagfinn Reiersol's Blog:
Real programming with PHP 5.3 (part 4): A more realistic example
Jun 05, 2009 @ 12:57:21

On the PHP in Action blog Dagfinn Reiersol continues his look at real programming examples using the upcoming PHP 5.3 release. In this new tutorial he focuses on making a classed based on the Range design pattern to paginate results.

A Range object is defined by just two values (three in my variation), so it might seem like too much to have an object just to keep these values, but as you can see from the example, a Range class can have behaviors to change and compare ranges.

He gives two code snippets - the first defines most of the methods of the class in the constructor (using the lambda functions) and the second uses a __call to catch the calls to the same methods but defined normally as private (making them inaccessible to the outside world).

tagged: lambda designpattern range tutorial

Link:

PHPClasses.org:
Upcoming PHP 5.3 features and beyond
Apr 28, 2009 @ 12:56:34

This new post to the PHPClasses.org blog take a look at some of the new features that will be included in the PHP 5.3 release (coming soon to a web server near you!) by way of an interview with Lukas Smith.

his article presents an interview with core PHP developer Lukas Kahwe Smith that has pushed many of the new features of PHP 5.3 as release manager. Lukas talks about PHP 5.3 new features such as lambda functions, closures and PHAR support. He also explains what are traits, which for now it is a feature that was left out of PHP 5.3. He also talks about future PHP 5.x and PHP 6, as well how anybody can help in the development of PHP to make it come out faster.

They talk about the PHP.net wiki, briefly touch on the PHP 5.3 feature updates, performance/memory usage in the new version, Lambda functions, closures and PHAR and what's to come with PHP 6.

tagged: lukassmith php5 upcoming php6 interview phar closures lambda memory usage

Link:


Trending Topics: