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

Laravel News:
Laravel Blade Components and Slots are coming to 5.4
Nov 16, 2016 @ 10:46:54

On the Laravel News site there's a post about an upcoming feature in the Laravel framework's Blade templating functionality: components and slots.

A new feature coming to Laravel 5.4 is the ability for you to add Components and Slots to Blade templates. This feature was inspired by Vue.js and allows you to simplify building HTML elements into reusable areas.

In most applications you have a master layout and then sub views that extend it. [...] Using the new Laravel Blade Components you can create [a template] with a special variable [for easy replacement].

The post then shows how to "reimagine" views using this slots and components functionality in a simple template/view example, replacing data based on names rather than it having to be passed to the view as data.

tagged: laravel blade template component slot data replacement framework

Link: https://laravel-news.com/2016/11/blade-components-slots/

Decouple Your Framework for Easy Replacement
Aug 12, 2016 @ 11:14:12

In this recent post to the In2it blog Michelangelo van Dam makes a recommendation to decouple your logic from your framework to make it easier in the future if you need to replace it.

Decouple your framework or library from your business logic for future upgrades or replacements through usage of interfaces. By separating your business logic completely from the tool used to glue all things together, you can replace your framework or upgrade to a newer version without much problems.

He talks about how it's common for applications to quickly become "good application turns into a cluster of code on top of a cluster of code". While the title suggests completely swapping out the underlying framework, he shifts it to talk more about updates to the current framework, especially ones that would break non-decoupled functionality. He then covers the ideals of "interoperability" between PHP packages based on common interfaces (like the PSRs) and how following a similar idea can help decouple your code to prevent hard work for future potentially breaking changes.

tagged: framework replacement changes interoperability dependencyinjection example

Link: https://www.in2it.be/2016/08/decouple-framework-library-easy-replacement/

David Sklar:
Fixing Broken UTF-8
Aug 27, 2015 @ 10:48:29

David Sklar has a post to his site showing you how to fix broken UTF-8 characters in content being passed through the normal string functions.

When working on the i18n bits of Learning PHP 7, I had a problem. My example showing how plain string functions such as strtolower() and strtoupper() mangle multibyte UTF-8 characters was making the book formatting/rendering pipeline barf. The processing tools are expecing nicely formatted, valid, UTF-8 encoded HTMLBook files. It didn’t like the mangled invalid UTF-8 characters in my example output.

To fix this, I wrote the following function to replace invalid UTF-8 sequences with the Unicode Replacement Character (U+FFFD).

He includes the code for this method that walks through the string, character by character, and checks the bytes it contains to see how it needs to be translated. There's plenty of comments in it too, explaining what it's doing as it goes along.

tagged: fix broken utf8 character function example unicode replacement

Link: http://www.sklar.com/php/2015/08/25/fixing-broken-utf8/

Replacing controllers with middleware
Apr 01, 2015 @ 09:53:50

Dracony has a new post to his site that suggests replacing controllers with middleware and how it relates to some of the current controller practices.

Middleware is now a very popular topic in the PHP community, here are some of my thoughts on the subject. [...] The idea behind it is “wrapping” your application logic with additional request processing logic, and then chaining as much of those wrappers as you like. So when your server receives a request, it would be first processed by your middlewares, and then after you generate a response it will also be processed by the same set.

After giving a few examples of what could be a good fit for use as middleware, he makes the suggestion to replace controllers. He talks about some of the problems that middleware brings with it and how to turn things around and write controllers as middleware (and not wrap them in it). He finishes with a mention of the work being done on PSR-7 (the HTTP Request proposal) and some thoughts on how it could fit into his middleware ideas.

tagged: middleware controller replacement opinion psr7 http

Link: http://dracony.org/replacing-controllers-with-middleware/

Matthew Setter:
Can VIM Ever Replace PHPStorm?
Mar 02, 2015 @ 10:54:13

In an interesting new post to his site Matthew Setter wonders if an IDE like PHPStorm can be replaced by VIM, a standard in the editor community for decades.

Is it reasonable, even practical, to expect that a 30 year old application can match a modern one? Is it conceivable to be able to code as well using VIM, said 30 year old application, as I can in PhpStorm, with all the IntelliSense-lead functionality PhpStorm offers?

He starts with some of his recent experience with the editor and some of the things he's (happily) found it can do he didn't know before. He includes a screenshot of a multi-pane view, explains what each is and what he can do with them. He points out that this example is Markdown documents but it can just as easily be used for code too. Finally he talks about the subject any PHPStorm user wants to know about, the IntelliSense functionality. Unfortunately, while there are some tools he mentions that can do similar things, they don't provide the context PHPStorm can deliver.

He ends the post with an interesting question: is IntelliSense the right approach? He wonders if having more context is a better answer rather than just the auto-complete handling IntelliSense offers.

tagged: vim editor replacement phpstorm opinion autcomplete context screenshot

Link: http://www.matthewsetter.com/can-vim-ever-replace-phpstorm/

SitePoint PHP Blog:
A Look at Hack, the PHP Replacement in HHVM
Feb 20, 2014 @ 10:43:27

On the SitePoint PHP blog today Victor Berchet introduces HACK, the "replacement" for PHP inside the HHVM (HipHop Virtual Machine) from Facebook's development group. This is part two in the HACK/HHVM series.

HACK is an evolution of the PHP language designed to be safer, to enable better performance and to improve developer efficiency. Note that both HACK and PHP are equally supported by the HHVM. Despite the fact that HACK is in use at Facebook on all the production servers, only little info has leaked for now. In a nutshell, HACK is Facebook's PHP6 – it proposes to fix most of what's wrong with PHP today, while adding some new features like static typing along the way.

He starts with a look at typing and the static typing HACK uses instead of the dynamic typing of the normal PHP language. He mentions the "gradual typing" functionality, strict mode and the use of annotations. He also talks about XHP, the PHP extension that allows XML fragments to be translated into valid PHP (example included). He finishes off the post looking at a few other topics including asynchronous code execution, FastCGI support and the integrated debugger.

tagged: hack facebook hhvm language introduction replacement

Link: http://www.sitepoint.com/look-hack-php-replacement-hhvm

Matt Farina's Blog:
SplFixedArray, An Underutilized PHP Gem
Sep 09, 2011 @ 10:43:11

Matt Farina has a new post today looking at an "underutilized gem" he's found in the offerings of the Standard PHP Library (SPL) - the SplFixedArray.

Arrays in PHP are not arrays per the typical array data type. Instead, as Matt Butcher recently pointed out arrays in PHP are similar to hashes in other languages. This can be a very important point to know when tracking down bugs in code and to programmers coming to PHP from other languages. But, what if we wanted something like a traditional array data type? Maybe something that preserved numeric order. Enter SplFixedArray.

He gives an example of using the SplFixedArray object versus the normal array variables in a simple PHP snippet showing the preservation of numbering order. He also touches on the memory consumption difference between the two, with the fixed array coming in quite a bit lower than the normal array data type (around 25% based on his basic testing). There are some catches to using it, though including incompatibility with array methods and the fact that it doesn't implement things like Iterator or Countable interfaces.

tagged: splfixedarray array replacement issues performance memory usage


How to remove getters and setters
Feb 23, 2011 @ 12:02:17

On DZone.com's Web Builder Zone Giorgio Sironi has posted a few methods you can use to help get rid of getters and setters in your OOP PHP applications.

Encapsulation is (not only, but also) about being capable of changing private fields. If you write getters and setters, you introduce a leaky abstraction over private fields, since the names and number of your public methods are influenced coupled to them. They aren't really private anymore:

To show his alternatives, he uses a sample "User" class with a whole list of private properties. There's initially a get/set for the nickname and password values, but he suggests a few replacements:

  • passing values in through the constructor
  • using the "Information Architect" pattern to have the most responsible method handle the value setting
  • the "Double Dispatch" method that uses dependency injection
  • using the Command pattern and changesets of data

He also briefly mentions the Command-Query Responsibility Segregation (CQRS) method, but doesn't have any code example to go with it.

tagged: getter setter opinion remove replacement


David Sklar's Blog:
Fast Multiple String Replacement in PHP
Oct 01, 2010 @ 10:09:40

David Sklar has a new post to his blog today sharing an extension he's created to make replacing strings in PHP a much faster task - Boxwood.

A straightforward way to do [string replacement] in PHP is to pass an array of words to look for and their replacements to a function like str_replace() or str_ireplace(). Or, similarly, use a regular expression that gloms the search terms together (and potentially checks word boundaries.) There are assorted WordPress plugins that work like this. The problem with this approach is that it's really slow. Especially if you have a lot of words you're looking for. The amount of time it takes to do the search and replace grows in proportion to the number of words you're looking for. This is particularly unfortunate because usually, none of the words are ever found!

With the Boxwood extension, the word list can be as long as needed because it uses an ordered tree structure (trie) to parse the entire tree only once so there's much less overhead.

tagged: multiple string replacement extension boxwood


Richard Heyes' Blog:
PEAR::DB replacement class updated
Jun 20, 2007 @ 08:43:00

Richard Heyes has made some updates to his PEAR::DB replacement class today, including:

  • Added numCols() function to the DB_result object
  • Added seek() method to the DB_result object
  • Enabled you to specify the fetchmode for getRow() and getAll()
  • Made the test script easier to navigate
  • Added reset() method to the DB_result object

You can find out more about the project here or just download this new version and check out the documentation to get you started.

tagged: peardb class replacement pear database abstraction peardb class replacement pear database abstraction