emGee Software Solutions Custom Database Applications

Share this

Planet PHP

People blogging about PHP
Updated: 1 day 1 hour ago

Websockets in PHP - Nomad PHP

Tue, 08/14/2018 - 06:09

October - US
Presented By
John Fansler
October 18, 2018
20:00 CDT

The post Websockets in PHP appeared first on Nomad PHP.

Categories: Web Technologies

206 Partial Content - Evert Pot

Tue, 08/14/2018 - 03:00

206 Partial Content is used for range requests. It’s possible for a HTTP client to request only portions of a resource using range requests.

Examples of this might include a large log resource for which a client only wants the last n bytes. Or maybe a video and the client doesn’t want to buffer more data than needed, or maybe a user is seeking through the video by fast-forwarding.

If a client issued a range request, and the server is able to handle this feature, it can indicate to the client that it’s sending back only certain ranges with the 206 Partial Content status, and Content-Range headers.

The following request asks the server for a portion of a video:

GET /video.mp4 HTTP/1.1 Range: bytes=1048576-2097152

A server supporting this could respond as follows:

HTTP/1.1 206 Partial Content Content-Range: bytes 1048576-2097152/3145728 Content-Type: video/mp4 ...

If a server doesn’t support Range requests, it will just return a 200 OK and send back the entire video. A client will know that the server didn’t support it via this status code and the omission of the Content-Range header.

References
  • RFC7233 - (HTTP/1.1): Range Requests
Categories: Web Technologies

More code comments - Matthias Noback

Mon, 08/13/2018 - 05:10

Recently I read a comment on Twitter by Nicola Poša:

I guess the discussion on my thread is going in the wrong direction because I left out a crucial hashtag: #NoCommentsInCode - avoid comments in code, write descriptive classes, methods, variables.https://t.co/MuHoOFXCvV

— Nikola Poša (@nikolaposa) July 13, 2018

He was providing us with a useful suggestion, one that I myself have been following ever since reading "Clean Code" by Robert Martin. The paraphrased suggestion in that book, as well as in the tweet, is to consider a comment to be a naming issue in disguise, and to solve that issue, instead of keeping the comment. By the way, the book has some very nice examples of how comments should and should not be used.

Comments as a naming issue

I think in most cases, indeed, we don't need comments. Common suggestions are, like Nicola suggests as well:

  • Introduce a variable with a meaningful name, which can hold the result of a complicated expression.
  • Introduce a method with a meaningful name, which can hide a piece of complicated logic.

The "hiding" that a method does, provides you with two interesting options:

  1. The method name can summarize a number of statements.
  2. The method name can represent a concept that is of a higher level than the lower-level things that are going on inside the method.

Option 1 is useful, but if you can go for option 2, it'll be the most valuable option.

Consider Nicola's example:

if ($date->hour < 9 || $date->hour > 17) { //Off-hours? }

Option 1 would entail something like:

if ($this->hourIsBetween9And17($date->hour)) { //Off-hours? }

Option 2 would be much more interesting and useful:

if ($this->isAnOffHour($date->hour)) { }

This introduces abstraction, where the client doesn't care about the concrete details of determining whether or not a certain hour is an off-hour, but only wants to know if the given hour is indeed an off-hour.

#NoComments?

I realized I'm always applying the following rule: if I feel like writing a comment, I look for a way in which I can change the code so that I don't have to write a comment anymore. Also, when I encounter a comment in an existing code base, I apply the same rule, and look for ways to remove the comment. Because, as you may know, code can't lie, but comments can, so let's get rid of them before they become a risk.

However, contrary to my own advise, I also realized that I've actually been writing more and more comments over the past few months. So what's going on?

A little soul-searching revealed that I've been adding code comments that can be categorized as follows:

  1. Explanatory comments: ~70%
  2. TODO comments: ~20%
  3. WTF comments: ~10%
Explanatory comments

I've been adding explanatory comments to parts in the code that need an explanation, or a canonical description of the situation. Most often these comments can be found near a couple of statements in the domain model. The comment then explains why a certain business rule should be applied, and how the code beneath the comment actually accomplishes this. Of course, I try to match the words in the comment with the words in the code itself, so they are tied together.

Before adding an explanatory comment, I often try to rephrase the explanation as a test first. This is usually the better option, since it will make sure that the code and its documentation will never diverge. The test is the documentation that describes a piece of the code, and if the code is no longer truthful to that description, the test will fail. An even better option is to apply behavior-driven development, where tests can actually be written as stories with examples, and they will serve as automated tests at the same time.

Still, some things just need a whole paragraph of explaining, and in that case, I happily put the explanation in the code, and take my time (and a long-form commenting style like /* ... */) to explain what's going on.

"Here's a question: why don't you create a separate document, instead of a code comment?"

Good question; i

Truncated by Planet PHP, read more at the original (another 4676 bytes)

Categories: Web Technologies

Beanstalkd Munin PHP 1.0.0 - PHP-GTK Community

Sun, 08/12/2018 - 12:10

Créé en 2014 sur la base des plugins Munin d'AirBnB pour Beanstalkd en Python, le package OSInet beanstalk-munin-php est dorénavant disponible en version 1.0.0.

Ces plugins pour Munin 2.x permettent la surveillance des métriques suivantes:

  • bs_cmd_rate.php : fréquence des commandes put, reserve, reserve_timeout, delete, touch, release et bury
  • bs_connections.php : gauge des connexions ouvertes
  • bs_jobs_rate.php : dérivée du nombre total de travaux traités
  • bs_queue_age.php : gauge de la durée d'attente des travaux dans les tubes (files)
  • bs_queue_size.php : gauge du nombre de travaux par état: ready, urgent, reserved, delayed, buried
  • bs_timeouts.php : dérivée du nombre de timeouts par unité de temps

Ces plugins sont publiés sous licence Apache APL-2.0, et conformes aux normes PHP-FIG: PSR1, PSR2, PSR12 et au standard de codage Zend.

Categories: Web Technologies

Interview with Chris Rowe - Voices of the ElePHPant

Wed, 08/08/2018 - 07:07

Show Notes Audio This episode is sponsored by .

The post Interview with Chris Rowe appeared first on Voices of the ElePHPant.

Categories: Web Technologies

205 Reset Content - Evert Pot

Tue, 08/07/2018 - 08:00

205 Reset Content is somewhat similar to 204 No Content. It’s especially meant for hypertext applications.

Imagine if a user is filling in a html form, and this form gets submitted to the server.

If the server sends back a 204, a browser could act on this by telling the user the form submission was successful and NOT refresh the page.

If the server sent back a 205, a browser can reset the form, similar to a a reset button a html form:

<input type="reset" />

Browsers could implement this, but at least according to an article from 2008 by Ben Ramsey, they don’t. Implementing this status by a HATEOAS client could definitely still make sense though.

Example response:

HTTP/1.1 205 Reset Content Server: foo-bar/1.2 Connection: close References
Categories: Web Technologies

How to setup PhpStorm with Xdebug on Docker [Tutorial Part 2] - Pascal Landau

Mon, 08/06/2018 - 05:00

In the second part of this tutorial series on developing PHP on Docker we're taking a good hard look at PhpStorm, Xdebug and how to run and debug scripts from within PhpStorm on Docker.

And just as a reminder, the first part is over at Setting up PHP, PHP-FPM and NGINX for local development on Docker.

Note: The setup that I am going to use is for demonstration purposes only! I do not recommend that you use it "as is" as your development setup. Some problems that I won't solve here include:

  • everything is owned by root (no dedicated user; that will in particular be problematic for linux users)
  • SSH login credentials are hard-coded in the container (inherently insecure)
  • host.docker.internal will only exist for Windows and Mac users, NOT for unix users

There will be a another part of this series that will deal with all of those (and some more common) problems and aims at providing a consistent development environment for all developers in a team (regardless of the OS they are using). Please subscribe to the RSS feed to get automatic notifications when that part comes out :)

Table of contents Setup: The docker containers

We will only need the php-cli container for this part. Luckily, we already have a good understanding on how to create the container, although we'll need to make some adjustments to make everything work smoothly with PhpStorm. I'm gonna walk you through all the necessary changes, but I'd still recommend to clone the corresponding git repository docker-php-tutorial (unless you've already done that in part 1), checkout branch part_2 and build the containers now.

As in part 1, I'm assuming your codebase lives at /c/codesbase:

cd /c/codebase/ git clone https://github.com/paslandau/docker-php-tutorial.git cd docker-php-tutorial git checkout part_2 docker-compose docker-compose build

Further, make sure to open /c/codebase/docker-php-tutorial as a project in PhpStorm.

In general, there are two ways to run PHP from PhpStorm using Docker:

  1. via the built-in Docker setup
  2. via Deployment Configuration (treating docker more or less like a VM)
Run PHP via built-in Docker setup

This is the "easier" way and should mostly work "out of the box". When you run a PHP script using this method, PhpStorm will start a docker container and configure it automatically (path mappings, network setup, ...). Next, the script in question is executed and the container is stopped afterwards.

Enable docker to communicate on port 2375

Open the Docker Setting in tab "General" and activate the checkbox that says "Expose daemon on tcp://localhost:2375 without TLS".

Configure Docker Server in PhpStorm

In PhpStorm, navigate to File |

Truncated by Planet PHP, read more at the original (another 21610 bytes)

Categories: Web Technologies

How to setup PhpStorm with Xdebug on Docker [Tutorial Part 2] - Pascal Landau

Mon, 08/06/2018 - 05:00

In the second part of this tutorial series on developing PHP on Docker we're taking a good hard look at PhpStorm, Xdebug and how to run and debug scripts from within PhpStorm on Docker.

And just as a reminder, the first part is over at Setting up PHP, PHP-FPM and NGINX for local development on Docker.

Note: The setup that I am going to use is for demonstration purposes only! I do not recommend that you use it "as is" as your development setup. Some problems that I won't solve here include:

  • everything is owned by root (no dedicated user; that will in particular be problematic for linux users)
  • SSH login credentials are hard-coded in the container (inherently insecure)
  • host.docker.internal will only exist for Windows and Mac users, NOT for unix users

There will be a another part of this series that will deal with all of those (and some more common) problems and aims at providing a consistent development environment for all developers in a team (regardless of the OS they are using). Please subscribe to the RSS feed to get automatic notifications when that part comes out :)

Table of contents Setup: The docker containers

We will only need the php-cli container for this part. Luckily, we already have a good understanding on how to create the container, although we'll need to make some adjustments to make everything work smoothly with PhpStorm. I'm gonna walk you through all the necessary changes, but I'd still recommend to clone the corresponding git repository docker-php-tutorial (unless you've already done that in part 1), checkout branch part_2 and build the containers now.

As in part 1, I'm assuming your codebase lives at /c/codesbase:

cd /c/codebase/ git clone https://github.com/paslandau/docker-php-tutorial.git cd docker-php-tutorial git checkout part_2 docker-compose docker-compose build

Further, make sure to open /c/codebase/docker-php-tutorial as a project in PhpStorm.

In general, there are two ways to run PHP from PhpStorm using Docker:

  1. via the built-in Docker setup
  2. via Deployment Configuration (treating docker more or less like a VM)
Run PHP via built-in Docker setup

This is the "easier" way and should mostly work "out of the box". When you run a PHP script using this method, PhpStorm will start a docker container and configure it automatically (path mappings, network setup, ...). Next, the script in question is executed and the container is stopped afterwards.

Enable docker to communicate on port 2375

Open the Docker Setting in tab "General" and activate the checkbox that says "Expose daemon on tcp://localhost:2375 without TLS".

Configure Docker Server in PhpStorm

In PhpStorm, navigate to File |

Truncated by Planet PHP, read more at the original (another 21610 bytes)

Categories: Web Technologies

Negative architecture, and assumptions about code - Matthias Noback

Mon, 08/06/2018 - 01:25

In "Negative Architecture", Michael Feathers speaks about certain aspects of software architecture leading to some kind of negative architecture. Feathers mentions the IO Monad from Haskell (functional programming) as an example, but there are parallel examples in object-oriented programming. For example, by using layers and the dependency inversion principle you can "guarantee" that a certain class, in a certain layer (e.g. domain, application) won't do any IO - no talking to a database, no HTTP requests to some remote service, etc.

[...] guarantees form a negative architecture - a set of things that you know can’t happen in various pieces of your system. The potential list is infinite but that doesn’t stop this perspective from being useful. If you’re using a particular persistence technology in one part of your system, it is valuable to be able to say that it is only used in that place and no place else in the system. The same can be true of other technologies. Knowing what something is not able to do reduces the number of pitfalls. It puts architecture on solid ground.

I find that this is a great advantage of making any kind of agreement between team members: about design principles being applied, where to put certain classes, etc. Even making a clear distinction between different kinds of tests can be very useful. It's good to know that if a test is in the "unit test" directory, it won't do any IO. These tests are just some in-memory verifications of specified object behavior. So we should be able to run them in a very short amount of time, with no internet connection, no dependent services up and running, etc.

A negative architecture is what's "between the lines". You can look at all the code and describe a positive architecture for it, but given certain architecture or design rules, there's also the background; things we can be sure will never happen. Like a User entity sending an email. Or an html template doing a database migration. Or an HTTP client wiping out the entire disk drive. And so on and so on; I'm sure you can think of some other funny stuff that could happen (and that would require instant, nightmarish fixing).

It all sounds really absurd, but tuned down a little, these examples are actually quite realistic if you work with a legacy project that's in a bad shape. After all, these scenarios are all possible. Developers could've implemented them if they wanted to. We can never be certain that something is definitely not happening in our system. In particular if we don't have tests for that system. We can only have a strong feeling that something won't be happening.

The legacy project I'm working on these days isn't quite as bad as the ones we're fantasizing about now. But still, every now and then I stumble upon an interesting class, method, statement, which proves that my picture of the project's "negative architecture" isn't accurate enough. This always comes with the realization that "apparently, this can happen". Let's take a look at a few examples.

"An object does nothing meaningful in its constructor"

I've learned over time to "do nothing" in my constructor; just to accept constructor arguments and assign them to properties. Like this:

final class SomeService { private $foo; private $bar; public function __construct($foo, $bar) { $this->foo = $foo; $this->bar = $bar; } }

Recently, I replaced some static calls to a global translator function by calls to an injected translator service, which I added as a new constructor argument. At the last line of the constructor I assigned the translator service to a new attribute (after all, that's what I've learned to do with legacy code - add new things at the end):

final class SomeService { // ... private $translator; public function __construct(..., Translator $translator) { // ... $this->translator = $translator; } }

However, it turned out that the method that needed the (now injected) translator, was already called in the constructor:

public function __construct(..., Translator $translator) { // ... $this->aMethodThatNeedsTheTranslator(...); $this->translator = $translator; }

So before I assigned the translator to its dedicated attribute, another method attempted to use it already. This resulted in a nasty, fatal runtime error. It made me realize that my assumption was that in this project, a constructor would never do something. So even though th

Truncated by Planet PHP, read more at the original (another 8202 bytes)

Categories: Web Technologies

Firefox removes RSS support - Evert Pot

Fri, 08/03/2018 - 12:45

gHacks recently reported that Mozilla plans to remove support for RSS & Atom related features. Specifically Live Bookmarks and their feed reader.

The reasons cited are the lack of usage. This doesn’t completely surprise me. Both of the features don’t fit that well with modern workflows. There’s not a lot of people using bookmarks actively except through auto-complete, and the reader interface never really was as effective as just looking at a website. Honestly the day that Mozilla didn’t show the feed-discovery button by default in the address bar is the day they killed it.

Firefox feed reader:

The 2000’s vs Today

I can’t help being sad about this though. For a little while feeds were a very prominent and exciting feature of the web. More than an effective tool to replace mailing lists, they represented an idea that anyone can start a website and become part of the “blogosphere”. The dreams were pretty big in the 2000’s. Aside from standard protocols for getting updates from websites, many platforms supported standard API’s for authoring blogs, and API’s for federated commenting and even API’s for federated identity (OpenID).

At its height, even Microsoft Word got integration with these standard blogging (Metaweblog) APIs for easy authoring.

Today it feels like a lot of that dream has died, and we’ve moved back to censored corporate-controlled content-silos such as Facebook and Twitter. Each with their proprietary API’s, incompatable with anything besides themselves and no interest to open up their networks to federation with other providers. These large vendors are worried about losing control of where content lives and where your firends are, because once they lose the network effect the only thing that’s left is a steaming pile of garbage where nobody really wants to hang out anyway.

So I’m sad to see this feature being removed from Firefox. It wasn’t a really a fleshed out or well maintained feature. It’s recoverable with add-ons, but what I really care about, is what the feature represents.

Mozilla’s mission is:

Our mission is to ensure the Internet is a global public resource, open and accessible to all. An Internet that truly puts people first, where individuals can shape their own experience and are empowered, safe and independent.

I think it’s important to say that I don’t necessarily advocate for preserving this feature. I want Mozilla to be a catalyst for taking control from corporate silos back to the individual. RSS & Atom might not be the key to this goal, but without a good replacement this doesn’t feel good. To me it goes against the mission that Mozilla set out to accomplish.

Categories: Web Technologies

PHP 7.3.0.beta1 Released - PHP: Hypertext Preprocessor

Wed, 08/01/2018 - 17:00
The PHP team is glad to announce the release of the fifth PHP 7.3.0 version, PHP 7.3.0beta1. The rough outline of the PHP 7.3 release cycle is specified in the PHP Wiki. For source downloads of PHP 7.3.0beta1 please visit the download page. Windows sources and binaries can be found on windows.php.net/qa/. Please carefully test this version and report any issues found in the bug reporting system. THIS IS A DEVELOPMENT PREVIEW - DO NOT USE IT IN PRODUCTION! For more information on the new features and other changes, you can read the NEWS file, or the UPGRADING file for a complete list of upgrading notes. These files can also be found in the release archive. The next release would be Beta 2, planned for August 16th. The signatures for the release can be found in the manifest or on the QA site. Thank you for helping us make PHP better.
Categories: Web Technologies

204 No Content - Evert Pot

Tue, 07/31/2018 - 08:00

204 No Content should be returned by a server when a request was successful, but there was no response body.

For most APIs, the following two responses are the same:

HTTP/1.1 200 OK Content-Length: 0 HTTP/1.1 204 No Content

The second format has a slight preference by most API designers. The reason there is a separate status-code, is because the 204 No Content response can signal a hypermedia client to not change the document and just stay on the current ‘view’. For example, a client might ‘save’ a document temporarily and not refresh to a new page.

Personally I have not really seen this behavior, but I can absolutely see this as a real possibility for a true HATEOAS client.

However, most of the time, 204 is simply used as a stand-in for 200 without a response body.

204 No content is sometimes misunderstood by implementors of HTTP APIs to indicate that the target resource ‘no longer has any contents’ after a DELETE request. However, other successful requests that don’t have a response body should also have the 204 No Content status, including PUT and POST.

References
Categories: Web Technologies

Atlas ORM Integration with Symfony - Paul M. Jones

Tue, 07/31/2018 - 05:34

Are you using Symfony 4? Do you want to use Atlas with it? We now have a Symfony bundle and Flex recipe that makes installation and integration a breeze. Two commands and one .env file edit, and you’re ready to go:

composer config extra.symfony.allow-contrib true composer require atlas/symfony ~1.0

Build out all your mapper files from your database tables with a single command:

php bin/console atlas:skeleton

Then let Symfony inject the Atlas ORM object in your controller or application service constructors automatically (no further configuration needed):

<?php namespace App; use Atlas\Orm\Atlas; use App\DataSource\Thread\Thread use App\DataSource\Thread\ThreadRecord; class ApplicationService { public function __construct(Atlas $atlas) { $this->atlas = $atlas; } public function fetchThreadById($thread_id) : ThreadRecord { return $this->atlas->fetchRecord(Thread::class, $thread_id); } }

That’s it – you can now use Atlas for all the heavy lifting of your database work:

If you’re looking for a good persistence model data mapper, give Atlas a try!

Categories: Web Technologies

Never type hint on arrays - larry@garfieldtech.com

Fri, 07/27/2018 - 20:18
Never type hint on arrays Submitted by Larry on 27 July 2018 - 11:18pm

Let's be controversial: In modern PHP, you should never type-hint an array.

Before you start throwing tomatoes, hear me out.

Continue reading on Steemit

Categories: Web Technologies

203 Non-Authoritative Information - Evert Pot

Tue, 07/24/2018 - 08:00

203 Non-Authoritative Information is a status-code that might be used by a HTTP proxy.

A HTTP Proxy sits in the middle between a client and a server (origin). In some cases a HTTP Proxy might make changes to the response before it reaches the client.

Perhaps because the Proxy converts the format, or maybe it adds something to the html body.

For these situations a proxy can indicate that it changed something by changing the status-code to 203.

However, this status-code has some issues. If a proxy changed the status-code, it’s no longer possible to see what the original status code was. Maybe it was a 200, or 201 or 404 before. That information is now lost.

The RFC actually suggest to use the Warning header for this instead. The warning header can be set to the code 214 Transformation applied. The benefit of a warning code over a status-code is that it can be set without obscuring what the original status code was.

I would not recommend using 203, and I’m not aware of any software that does. A Warning header is better.

Example using 203 Non-Authorititative Information:

HTTP/1.1 203 Non-Authoritative Information Content-Type: text/plain Content-Length: 515 ...

Example using Warning header

HTTP/1.1 200 OK Content-Type: text/plain Warning: 214 proxy.example.org "We censored the response. srry" Content-Length: 515 ...

Even though the Warning header is actually required when proxies modify the response, most proxies don’t appear to include it.

Random fact: Non-Authoritative Information has 10 syllables. It shares the #1 spot for most syllables with another status code.

References
Categories: Web Technologies

PHP 7.1.20 Released - PHP: Hypertext Preprocessor

Thu, 07/19/2018 - 17:00
The PHP development team announces the immediate availability of PHP 7.1.20. This is a security release. Several security bugs have been fixed in this release. All PHP 7.1 users are encouraged to upgrade to this version. For source downloads of PHP 7.1.20 please visit our downloads page, Windows source and binaries can be found on windows.php.net/download/. The list of changes is recorded in the ChangeLog.
Categories: Web Technologies

Security Vulnerability Announcement: html_QuickForm - Official Blog of the PEAR Group/PEAR President

Thu, 07/19/2018 - 11:22

A vulnerability in the html_QuickForm package has been found which potentially allows remote code execution.

A new release of the package is available which fixes this issue. One is strongly encouraged to upgrade to it by using:

$ pear upgrade html_QuickForm-3.2.15

Thanks to Patrick Fingle and the CiviCRM Security Team who reported this issue.

Categories: Web Technologies

PHP 5.6.37 Released - PHP: Hypertext Preprocessor

Wed, 07/18/2018 - 17:00
The PHP development team announces the immediate availability of PHP 5.6.37. This is a security release. Several security bugs have been fixed in this release. All PHP 5.6 users are encouraged to upgrade to this version.For source downloads of PHP 5.6.37 please visit our downloads page, Windows source and binaries can be found on windows.php.net/download/. The list of changes is recorded in the ChangeLog.
Categories: Web Technologies

Pages