PHP Coalface

Here's the main RSS feed from phpdeveloper.org:

PHPDeveloper.org
Benjamin Eberlei: Explicit Global State with Context Objects on 24 March 2017

In a post to his site Benjamin Eberlei looks at using something called "context objects" and how they can be used as an alternative to true global state.

Global State is considered bad for maintainability of software. Side effects on global state can cause a very nasty class of bugs. Context objects are one flavour of global state. For example, I remember that Symfony1 had a particularly nasty context object that was a global singleton containing references to very many services of the framework.

As with every concept in programming, there are no absolute truths though and there are many use-cases where context objects make sense. This blog posts tries to explain my reasons for using context objects.

He starts by getting everyone on the same page by defining a context - the "circumstances in which something can be fully understood". He then moves into the world of context objects, talking about how they encapsulate the information other objects need to execute. They're essentially "container" objects that allow for more control that something like the normal PHP superglobals. From there he helps you define what kind of context objects you might need in your application and provides a real-world example from his own experience at .

SitePoint PHP Blog: Parallel Programming with Pthreads in PHP – the Fundamentals on 24 March 2017

The SitePoint PHP blog has posted a tutorial that in PHP. In their examples they make use of the to help bring simpler parallel programming to the language (otherwise you'd have to do odd things with shell commands and foreground/background controls).

PHP developers seem to rarely utilise parallelism. The appeal of the simplicity of synchronous, single-threaded programming certainly is high, but sometimes the usage of a little concurrency can bring some worthwhile performance improvements.

In this article, we will be taking a look at how threading can be achieved in PHP with the . This will require a ZTS (Zend Thread Safety) version of PHP 7.x installed, along with the pthreads v3 installed.

Despite the article being about the use of pthreads, it starts out talking about when not to use it, possibly saving you some time in the long run. With that out of the way it then starts in on the handling of "on-off tasks" with an example of fetching the "title" value from Google.com. This is then enhanced showing how to use the "Threaded" base class to define other classes that can be used inside of threads. The article moves on covering other topics including:

  • recycling threads
  • pthreads and (im)mutability
  • synchronization of threads

Each item in the list comes with plenty of example code showing you how to create the classes that execute the threads and the output they should generate.

Zend Framework Blog: Error Handling in Expressive on 24 March 2017

The Zend Framework blog has a new tutorial posted by Matthew Weier O'Phinney covering with a few examples making use of some custom middleware and logic.

One of the big improvements in Expressive 2 is how error handling is approached. While the covers the feature in detail, more examples are never a bad thing!

In their example they're creating an API resource that returns a list of book details (ones the user has read). The goal is to use the existing error handling for everything except the custom exceptions they want to throw but keep with the JSON handling throughout. First the middleware to handling the API request is shown, complete with sorting and pagination. Then come the custom exception examples for invalid requests and server issues. These exceptions are then put into the format with the help of another middleware. This then all tied together with the nested middleware handling Expressive provides and an example of the end result is included.

Site News: Popular Posts for This Week (03.24.2017) on 24 March 2017

Popular posts from PHPDeveloper.org for the past week:

TutsPlus.com: Building With the Twitter API: Creating Friends to Follow on 23 March 2017

The TutsPlus.com site has posted the latest tutorial in their "" series showing how to, in a Yii2 application, automatically via the Twitter API. You've probably seen this in several services that offer suggestions of followers to add to your list.

Today I'll guide you through using the Yii2 Framework for PHP to access the Twitter API and automate adding friends to people's Twitter accounts. (If you'd like to learn more about Yii2, check out our parallel series .)

And, I've created a website, , which will let you demonstrate the feature by adding prominent women on Twitter for your account to follow.

The tutorial starts with links to some of the other Twitter tutorials that have been posted in the past and how things have evolved to make it easier in a Yii2 application. He starts by helping you get the installed and lists some of the goals of the end result. The code is included to authorize the user and handle the callback once they've approved the app in the normal OAuth flow. It then shows how to connect via the API using that user's information, load profiles for the suggested users and link them as a friend. The tutorial finishes with a look at performance and a bit of code used to handle the backend processing of the request instead of performing it in real time.

Also from phpdeveloper.org, here are some Security links:

No channel data


Comments

It's quiet in here...Add your comment