Variables are born, they live their lives, and then they die. But beware! (Cue spoopy theme music.) Zombie variables lurk in the simplest of PHP.
Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts
Tuesday, October 30, 2018
Friday, November 11, 2016
Bypassing private and protected visibility in PHP
Members declared protected can be accessed only within the class itself and by inherited classes. Members declared as private may only be accessed by the class that defines the member.
This is true only in an academic sense: code outside the object can still get and set private and protected members. As usual in PHP, all it takes is a little magic.
Thursday, March 3, 2016
Evoking all possible test failure modes in PHPUnit
When you're writing your own PHPUnit test listener, you need a test case that evokes all the different PHPUnit test states. Here's you go:
<?php
class EvokesTest extends \PHPUnit_Framework_TestCase
{
public function test_pass()
{
}
public function test_fail()
{
$this->fail(__FUNCTION__);
}
public function test_error()
{
throw new \RuntimeException(__FUNCTION__);
}
public function test_skipped()
{
$this->markTestSkipped(__FUNCTION__);
}
public function test_incomplete()
{
$this->markTestIncomplete(__FUNCTION__);
}
public function test_risky()
{
throw new \PHPUnit_Framework_RiskyTestError;
}
}
Thursday, January 21, 2016
Using vim to replace string functions with their multi-byte equivalent
The PHP INI option mbstring.func_overload override certain string functions (like strpos, substr, etc.) with multi-byte aware implementations. This makes it super easy to migrate a legacy code base to UTF-8, but immediately restricts interaction with vendor products (Symfony, Net_DNS2, etc.).
The proper integration is to manually replace string functions with their multi-byte equivalent. In one legacy code base I'm improving, there are on the order of 10k instances of these functions. I want to change and verify each replacement, but I don't want to type much. Time for some vim-fu:
:argdo %s/strpos/mb_strpos/gc | wn
This performs a confirmed find and replace, writes the change to disk, then moves on to the next file.
Monday, January 11, 2016
[Proposed] Elephpant Etiquette
Yes, I do believe PHP internals needs a guide to etiquette. But, no, not a code of conduct. Internals is a decades (plural) old cathedral-like meritocracy. There is no benevolent dictator. There is no functional oversight group. No rigorous process (like Go has) will work in the internals ecosystem.
Anthony's draft sets the stage, but I don't think it'll draw the crowds. For that, we need a moderate approach that emphasizes the definition of acceptable behavior while limiting the authoritative scope. Here's my second attempt at a custom-fit "code of conduct" roughly based on the one from Go:
Anthony's draft sets the stage, but I don't think it'll draw the crowds. For that, we need a moderate approach that emphasizes the definition of acceptable behavior while limiting the authoritative scope. Here's my second attempt at a custom-fit "code of conduct" roughly based on the one from Go:
Friday, January 8, 2016
PHP Contributor Etiquette
I was the first to publically +1 the Code of Conduct RFC. I'd love to see a policy that fosters diversity and inclusion, because damn the PHP crowd is startlingly similar. But, after hearing the arguments, I can't endorse the code as written. It's too focused on process and punishment. I believe we need to look at this from a definition of rights and responsibilities. This is my attempt.
Friday, November 13, 2015
Wielding PHP magic with the Callable Object Pattern
The PHP magic method
__invoke provides a powerful way to encapsulate functionality while separating state from results and errors.
Tuesday, June 2, 2015
Wednesday, May 27, 2015
Shaking out dependencies in PHPUnit unit tests
Isolation is a unit test ideal. You should, in theory, be able to run them in a random order, or in parallel, with no effect on the results. PHPUnit provides no options to do this, but, fortunately, two neat GNU command line utilities can help.
Thursday, January 15, 2015
Memoization 2.0
You might think "Memoization" is a typo, but it's not. Memoization is a simple technique to improve software performance. Unfortunately, the technique relies upon repetitive boilerplate. In this article, I show you how to use memoization without any boilerplate code.
