Class Ando_Function for PHP

EDIT: https://github.com/aercolino/ando-php

This is a simple class for creating generic callbacks in PHP. There are a couple of reasons for using this class instead of the usual ´array( $container, $function )´ method.

First, the usual method doesn’t allow you to run the called code with more arguments than those provided by the calling code.

Second, this class resolves to the usual method when no extra arguments are provided, thus making it transparent at run time. This could seem useless, but, for free, we get much easier to spot callback calling points.

{[ .class-func | 1.hilite(=php=) ]}

Here are some tests.

{[ .tests | 1.hilite(=php=) ]}

Screen Shot 2014-10-28 at 16.53.43

(previous permalink: class-func-for-php)

Class RegularExpression for PHP

UPDATE

This is a small class for handling Regular Expressions in PHP. I often have to use backreferences and it’s very cumbersome to compose simple expressions with them into more complex ones, because I have to manually count all capturing groups many times in a row. This class basically solves that issue for me.

{[ .class | 1.hilite(=php=) ]}

 

How to program for the future in PHP

TL;DR: Refrain from ´private´ methods.

Still in version 4.0 of WordPress ´wp-includes/wp-db.php´ (but introduced in changeset 27075) we find this setup:

{[ .do_query | 1.hilite(=php=) ]}

It’s quite unfortunate that ´_do_query´ was declared ´private´ because if you inherit from ´wpdb´ (see the header of the class) and override ´public function query´ then you also have to override ´_do_query´ just because.

In fact, due to how private functions work in PHP (they can only be called from methods of the same class that defines them), you cannot call the inherited ´function _do_query´ from your overridden ´function query´, which instead is the most natural thing to do.

If possible, always prefer ´protected´ to ´private´ in PHP. If it had been declared ´protected function _do_query´ I could have called its inherited code from my ´public function query´.