21 Aug 2017

feedPlanet PHP

Are Bitwise Operators Still Relevant in Modern PHP?

Many of you probably scratched your heads reading this title. "Bitwhat?"

In this article, we'll look at what bitwise operators are, and whether or not their use is still relevant in this modern age of computing.

></p> <h2>Example Use Case</h2> <p>I'd been trying to <em>really</em> understand bitwise operators for a long time, until an example made it click for me. So that's what we'll do - dive straight into an example.</p> <p>Imagine you have a website on which a given user can have specific permissions. For example, a magazine like SitePoint:</p> <ul> <li>a author can CRUD drafts, and and edit their profile.</li> <li>an editor can, in addition to the above, CRUD drafts and finished posts, and CRUD author profiles.</li> <li>an administrator can, in addition to the above, add administrator permissions.</li> </ul> <p>Since a user can have multiple permissions, there are several ways of defining permissions in a database and the system using it.</p> <h3>The Double Join</h3> <p>Add roles, add permissions, attach permissions to roles in a join table, then create another join table and bind some roles to some users.</p> <p>This approach creates four extra tables:</p> <ul> <li>permissions</li> <li>roles</li> <li>permissions<->roles</li> <li>roles<->users</li> </ul> <p>Quite a bit of overhead. Imagine having to edit these or list them in the app regularly in some frequently visited lists. Only heavy caching would save this app from collapsing under heavy load.</p> <p>One advantage, though, is that by defining roles really well with intricate permissions, you only have to stick users into roles and you're good - it keeps that join table light and fast.</p> <h3>The Single Join</h3> <p>Add permissions, add a join table, attach some permissions to some users</p> <p>This approach creates two extra tables:</p> <ul> <li>permissions</li> <li>permissions<->users</li> </ul> <p>Much less overhead than the previous example, but you have many more entries in the join table because a user can have a LOT of permissions (just the CRUD for drafting is 4 permissions on its own). With a lot of users and a lot of permissions, this table can get heavy quickly.</p> <h3>The Column Stampede</h3> <p>Add a column into the users table for each permission, then make its datatype a tinyint(1) (basically a boolean) to check the permission as

Setting permissions for a user would then look something like this:

UPDATE `users` SET `editProfile` = 1, `deleteProfile` = 0, `createDraft` = 1, `publishDraft` = 0 ... WHERE `id` = 5

This approach adds no extra tables, but needlessly expands the table into gargantuan width, and requires a modification of the database every time a new permission is added. It's a fine approach for when you know you'll have at most two or three permissions for the foreseeable future, but shouldn't be used for anything more than that.

However, because the list of columns, when looked at from afar, resembles a binary number (1010), this approach is an excellent segway into another...

The Bitwise Approach

Before we dive deeper into this approach, let's have a crash course in binary.

Binary Numbers

All computers store data as binary: 0 or 1. So, the number 14 is actually stored as: 1110. How so?

Binary numbers are evaluated from right to left when calculating their value, just like real numbers. So the number 1337 means:

Because each digit in the decimal system (base 10) gets multiplied by 10. The first one is 1, the next one is 10, the next after that 100, the next 1000, etc.

Continue reading %Are Bitwise Operators Still Relevant in Modern PHP?%

21 Aug 2017 4:00pm GMT

ZendCon 2017

21 Aug 2017 11:01am GMT

18 Aug 2017

feedPlanet PHP

Why MVC doesn’t fit the web

[MVC is] a particular way to break up the responsibilities of parts of a graphical user interface application. One of the prototypical examples is a CAD application: models are the objects being drawn, in the abstract: models of mechanical parts, architectural elevations, whatever the subject of the particular application and use is. The "Views" are […]

18 Aug 2017 2:49pm GMT

Refactoring Done Right

November 2017 - US
Presented By

Brandon Savage
November 16, 2017
20:00 CST

The post Refactoring Done Right appeared first on Nomad PHP.

18 Aug 2017 4:01am GMT

API Tips From the Frontline

November 2017 - EU
Presented By

Anna Filina
November 16, 2017
20:00 CET

The post API Tips From the Frontline appeared first on Nomad PHP.

18 Aug 2017 4:01am GMT

17 Aug 2017

feedPlanet PHP

How to Master Your API Workflow with Postman

Building good APIs is hard, and anyone who had the chance to do so can relate to this. A project can easily grow to become a mess. One can keep trying to adopt an approach to make it more enjoyable, like trying a documentation-first workflow, but something always feels clumsy.

I was trying out Postman lately, a tool we've briefly covered before, and I discovered that they're doing a great job by providing an integrated environment for different components of an API, like authorization, testing, documentation, versioning, etc.

Postman logo

In this article, we're going to walk through the different features that Postman provides and how we can organize them to make our API workflow less painful.

Making Requests

The first step to discovering Postman is making a simple API request and seeing the response.

Simple request

From the screenshot above we can see that the view incorporates different elements. The top bar contains the request method (in this case GET), and right next to it we specify the endpoint to make the request to. If the request has some params we can specify them by clicking the Params button, which will open a table for entering them. When ready, we can click the send button to initiate the request.

The next section contains five tabs:

The bottom section contains all the details about the response (status, time and size). The four tabs are self explanatory, except for Tests which contains the tests result if we have any (more about this later).

Authorization

Postman supports all types of authorization, as we saw above. We're going to focus on token authorization set via header. You can read more about authorization here.

The header format that Postman will use is:

Authorization: Bearer <TOKEN>

Authorization header

Now, we'll go ahead and get a token we can use for our future requests. I'm testing on an in-development application of mine, but you can use any application you have lying around, or set one up quickly with Laravel as described here.

Not familiar with Laravel? Try our premium course - it's a comprehensive introduction, and you'll get the upcoming newer version of it automatically if you grab it in its current iteration!

Get token

At this point, we have a token to be used for querying data from the API as an authenticated user. However, copying and pasting the token for every request is tedious!

Environment Variables

This nice feature alleviates the pain of copy/paste and groups our variables in one place. An environment is an execution context: we may have a local environment, testing, staging, etc.

Postman has different scopes for variables:

The global variables are available everywhere, separately from the selected environment. You can read more in the documentation.

We need at least three variables for now:

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

17 Aug 2017 4:00pm GMT

PHP 7.2.0 Beta 3 Released

The PHP development team announces the immediate availability of PHP 7.2.0 Beta 3. This release is the third and final beta for 7.2.0. All users of PHP are encouraged to test this version carefully, and report any bugs and incompatibilities in the bug tracking 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. For source downloads of PHP 7.2.0 Beta 3 please visit the download page, Windows sources and binaries can be found at windows.php.net/qa/. The first Relase Candidate will be released on the 31th of August. You can also read the full list of planned releases on our wiki. Thank you for helping us make PHP better.

17 Aug 2017 12:00am GMT

16 Aug 2017

feedPlanet PHP

Preparing a Leanpub book for print-on-demand

16 Aug 2017 2:35pm GMT

15 Aug 2017

feedPlanet PHP

The “Micro” Framework As “User Interface” Framework

(The following is more exploratory than prescriptive. My thoughts on this topic are incomplete and in-progress. Please try to treat it accordingly.) tl;dr: "Micro" frameworks are better described as "user interface" frameworks; perhaps there should be corollary "infrastructure" frameworks; consider using two frameworks and/or two containers, one for the user interface and a separate one […]

15 Aug 2017 6:38pm GMT

PHPBot – Can a PHP Bot Help You Look up Documentation Faster?

I came across PHPBot the other day (not to be confused with Botman or Fondbot) - a "chatbot" which helps you look up PHP manual entries and generates example code for them.

If you follow me on Twitter, you know I don't believe in chatbots being chatbots - as someone who's built some for both commercial purposes and personal ones, and as someone who was around in the age of IRC auto-responding scripts, I see chatbots as a little more than a marketing fad designed to impress today's Snapchat generation. However, every now and then one will appear that's actually useful. Could this be the one? Let's take a look.

Bot welcoming image

Concept

The idea is that you use the text input on the page to enter a PHP related term (like "ksort") and the application will reply with an explanation and some example code, like so:

PHPBot's reply to <code/>ksort input

Different queries will produce different responses. So, if you ask it for echo:

PHPBot's reply to <code/>echo

Continue reading %PHPBot - Can a PHP Bot Help You Look up Documentation Faster?%

15 Aug 2017 4:00pm GMT