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

Davey Shafik:
Class Constants, How Do They Work? (Or: You Learn Something New Every Day...)
Jul 09, 2015 @ 08:24:43

Davey Shafik has posted a quick article to his site talking about class constants and something new he learned about them (and how it relates to the uniform variable syntax handling in PHP7).

Yesterday on Twitter there was a conversation started by Marco Pivetta regarding a particularly horrible bit of code he had spotted [that] creates a string using sprintf() by prefixing ::PARAMNAME with the result of calling get_class() on the $api variable, and then passes that string into constant() which will give you the value of a constant using it’s string name.

The conversation continued with comments from Elizabeth Smith about why this workaround was needed in the past. Davey also suggests that it won't work as expected if the input is an object and not a string but a test from Trevor Suarez proved that incorrect as well (it does work). He ends the post talking about PHP7 and showing how, thanks to the uniform variable syntax changes, this same kind of handling can be done in many other ways too.

tagged: class constant php7 uniform variable synatx getclass object string

Link: http://daveyshafik.com/archives/69193-class-constants-how-do-they-work-or-you-learn-something-new-every-day.html

Nikita Popov:
Internal value representation in PHP 7 - Part 2
Jun 22, 2015 @ 10:45:41

Nikita Popov has posted the second part of a series looking at how PHP 7 represents values internally. In the first part of the series the focus was on the major change from PHP 5: the zval updates and how they're allocated. This new post gets into more of the details on each of the types and how they're handled.

In the first part of this article, high level changes in the internal value representation between PHP 5 and PHP 7 were discussed. As a reminder, the main difference was that zvals are no longer individually allocated and don’t store a reference count themselves. Simple values like integers or floats can be stored directly in a zval, while complex values are represented using a pointer to a separate structure.

[...] In the following the details of the individual complex types will be discussed and compared to the previous implementation in PHP 5. One of the complex types are references, which were already covered in the previous part. Another type that will not be covered here are resources, because I don’t consider them to be interesting.

He goes through a few of the different types including strings and arrays and then gets into detail on how objects have changed from PHP 5 to PHP7. He also talks about "indirect zvals" (the IS_INDIRECT handling) that points to another zval instance rather than embedding it. Finally, he talks about two other constants, IS_CONSTANT and IN_CONSTANT_AST, and how they're used behind the scenes with some example code to illustrate.

tagged: internal value variable representation php7 zval types string array object constant ast

Link: http://nikic.github.io/2015/06/19/Internal-value-representation-in-PHP-7-part-2.html

Evert Pot:
PHP's callable typehint too loose?
May 07, 2015 @ 10:19:56

In his latest post Evert Pot wonders if the current implementation of the "Callable" type in PHP is too loose when it comes to what it will accept as a valid callable resource.

PHP got support for closures in version 5.3, and in PHP 5.4 we got support for a callable typehint. [...] All these little changes make it feel more comfortable to apply functional programming concepts to PHP, but occasionally we need to drop back to using less aesthetically pleasing code.

In his examples of "less aesthetically pleasing code" he shows a few different methods that work that aren't the typical closure or object arguments (like passing in an array of object+method name). He also shows an interesting option where you can use a string with a static method call (ex: "MyClass::method") and it will still be accepted. He points out that for this to work correctly in all situations, the call_user_func method should be used, not just calling the input directly.

tagged: callable typehint loose object method array variable iscallable calluserfunc

Link: http://evertpot.com/on-callables-and-closures/

Nikita Popov:
Internal value representation in PHP 7 - Part 1
May 06, 2015 @ 08:12:27

Nikita Popov has a new post, the first part of a series, talking about the internal handling of variables in PHP7 and how it has changed from the current/past methods.

My last article described the improvements to the hashtable implementation that were introduced in PHP 7. This followup will take a look at the new representation of PHP values in general. Due to the amount of material to cover, the article is split in two parts: This part will describe how the zval (Zend value) implementation differs between PHP 5 and PHP 7, and also discuss the implementation of references. The second part will investigate the realization of individual types like strings or objects in more detail.

He starts with an introduction to the "zval" struct type and how it relates to the "zvalue" union. He goes on to talk about reference counting on zvals and some of the reasoning/desire to change how these are handled. Finally, he gets to the zval handling coming in PHP7 and the fundamental change in zval handling - they're no longer "individually heap-allocated and no longer store a refcount themselves". This change has several advantages and including improved refcount handling and less pointers involved in determining the actual value. He includes an example of how this new zval structure is defined in PHP7 compare to the previous version too. The remainder of the post looks at other related issues including memory management, supported types and a major change to how variable references are handled.

tagged: internal value variable representation php7 zval zvalue memory reference

Link: http://nikic.github.io/2015/05/05/Internal-value-representation-in-PHP-7-part-1.html

Dan Miller:
Comparing the PHP 7 and Hack Type Systems
Apr 29, 2015 @ 08:31:43

Dan Miller, a core platform engineer at Etsy, has a new post on his personal site sharing his results from a comparison of the variable typing systems between the Hack language (created by Facebook) and what's coming in PHP7.

One of the exciting things about PHP 7, aside from the incredible performance improvements, is the introduction of scalar type hintingHack. I wanted to find out if you could execute the same code in PHP 7 and Hack, and what the differences in execution might be. Here's what I found out.

He starts by describing his setup (the versions of PHP7 and HHVM he's using) and shares a few simple examples. He uses the same(ish) code in both and points out some of the differences in what happens when each is executed. He also points out some of the differences in the features between the two (such as Hack not allowing for default arguments with a value of null). He tries a few more complicated things too, like mixing strict and non-strict files, and the findings. He ends the post with some of his overall thoughts of his results and his excitement about what the future holds for PHP7 and the hinting it will provide.

tagged: compare php7 hack type systems variable statictypehints hinting hhvm

Link: http://www.dmiller.io/blog/2015/4/26/comparing-the-php7-and-hack-type-systems

NetTuts.com:
A Practical Use of WordPress Global Variables
Sep 23, 2014 @ 11:54:09

On the NetTuts.com site there's a new post (a part of a series) that introduces you to the global variables available in WordPress and provides a practical use for them.

In the first article in this series, we reviewed an introduction to WordPress global variables. Specifically, we said that they are variables that hold a variety information that can be accessed from anywhere in the WordPress page lifecycle. We learned how to access a global variable and display various details about the latest post using the $post global variable. In today’s article, we will dive further into global variables by learning how to access them to display the author information.

In this post they focus on extracting user (author) data from the system via the "authordata" global variable. He shows an example of a print_r output of the data it contains and use this to filter posts, only showing three per-author.

tagged: wordpress global variable author information authordata

Link: http://code.tutsplus.com/tutorials/a-practical-use-of-wordpress-global-variables--cms-20854

Derick Rethans:
No to a Uniform Variable Syntax
Jul 17, 2014 @ 09:32:15

There's been an RFC that's recently made it through the voting process and was approved for inclusion in PHP6, the uniform variable syntax handling. When these changes are put into effect, some of the odd syntax you had to use for things like variable variables will be cleared up and standardized. However, Derick Rethans stood out as the only "no" vote, here's why...

As you might have heard, PHP developers voted on an RFC called "Uniform Variable Syntax". This RFC "proposes the introduction of an internally consistent and complete variable syntax". In general, this RFC argues for making PHP's parser more complete for all sorts of variable dereferences. [...] Thirty people voted for, and one against: Me. Does that mean that I am against a unified variable syntax? No, I am not. I am actually quite a fan of having a consistent language, but we need to be careful when this hits existing users.

He points out that there's known backwards compatibility breaks in the changes and this breaks the semantics of the language. While the BC breaks are understood, Derick suggests that this is one of the worst changes a language can make: "...and this is exactly why people whine that PHP breaks BC and does not care about its users".

tagged: rfc uniform variable syntax against vote semantics language

Link: http://derickrethans.nl/uniform-variable-syntax.html

DZone.com:
Practical PHP Refactoring: Encapsulate Downcast (and Wrapping)
Nov 04, 2013 @ 12:44:06

On DZone.com Giorgio Sironi has posted a refactoring recommendation around the handling of the data and types in your PHP code. He suggests the move from just a simple variable to a Value Object (noting that it's not really needed in PHP, but can provide a "richer interface" to work with the data).

Statically typed languages sometimes encounter the problem of downcasting: the compiler is only able to guarantee a basic type, and the object contained instead is an instance of a richer subtype. [...] You'll never need to downcast objects: variables can contain handlers to objects or even scalars without compile-time checks. Casting with (ClassName) is not even supported by the language (while casting a non-object with (object) will give you a stdClass.)

He starts by talking about scalar values in PHP and a simple form of downcasting - using the casting notation included in the language. From there he moves into the conversion into Value Objects and some of the updates (like docblocks) that would come with their use. He outlines some steps towards the conversion and provides an example set of scripts showing the conversion process.

tagged: refactor scalara value variable valueobject downcast

Link: http://css.dzone.com/articles/practical-php-refactoring-38

Nikita Popov:
Order of evaluation in PHP
Sep 25, 2013 @ 10:51:35

If you're the kind of person that wonders more about the internals of PHP and how it works "under the covers" you'll find this new post from Nikita Popov a good read. It talks about how PHP handles its order of operations in more complex evaluation statements.

At this point many people seem to think that the order in which an expression is evaluated is determined by operator precedence and associativity. But that's not true. Precedence and associativity only tell you how the expressions are grouped.[...] What does this tell us about the order of evaluation? Nothing. Operator precedence and associativity specify grouping, but they do not specify in which order the groups are executed.

He gives a few examples to illustrate his point including multiple increments of the same variable at one time and how it's the "fault" of the compiled variables that were introduced in PHP 5.1. He shows the opcode version of the same PHP userland code and talks briefly about how to avoid this odd functionality in your application.

tagged: order evaluation opcode compiled variable

Link: https://gist.github.com/nikic/6699370

Russell Walker:
Handling Global Data in PHP Web Applications
Sep 16, 2013 @ 12:31:07

Russell Walker has a post on his site sharing some suggestions about effectively dealing with global data in your PHP applications.

Almost every web application needs to handle global data. There are certain things that just have to be available throughout the entire code base, such as database connections, configuration settings, and error handling routines. As a PHP developer, you may have heard the mantra 'globals are evil', but this naturally begs the question 'what should I use instead of global variables?'

He includes four different options (five including the actual use of global variables):

  • Static classes
  • Singleton
  • Registry
  • Dependency injection

For each of the options he includes summaries of both the advantages and disadvantages as well as some sample code showing their use. Ultimately, he points out that it's up to the developer of the application which option fits best.

tagged: global variable data opinion options registry singleton dependencyinjection static

Link: http://russellscottwalker.blogspot.co.uk/2013_09_07_archive.html