Amateur Protagonist Alister Sneddon

I once almost won a procrastinating contest, but I turned up.


PHP Type Hinting In Functions

10th July 2014
By Alister Sneddon

Don't you hate it when PHP stops listening to type hinting in your function parameters? I came across this strange issue with the Elgg frame work.

Long story short, the parameters in my functions where not forcing the type which was required. Take for example the following code

function get_money_then_get_bitches(ElggUser $user, array $extra_params) {
	$to_return = array();

	// Amazing code which updates the array and returns a standard type

	return $to_return;
}

In this case ElggUser is a new Object we have created, also we are asking for an array. However in PHP it is possible to pass in NULL or FALSE for both parameters. Heck you can pass in whatever the hell you want.

The problem we ran into was we did not always have the correct object. In the Elgg framework passing in 0 or an empty object it tends to pull back everything it can. So you would not end up with the money and bitches for that user, but for every user on the system.

Thanks Elgg, you dicks.

Either way this is not the frameworks fault, it is our setup. The issue is with debugging and dealing with multiple developers. It really is not good enough to leave a comment above your function. If your variable/parameter must be a set type then take it upon yourself to detect and deal with any problems.

I would always recommend setting a variable at the start of your function which you intend to return. The next thing you should do is check the parameters which have been passed in.

Let's improve the function we had before to account for any bad types. We will not force any type casting but we will check the type hint to ensure it is as expected.

function get_money_then_get_bitches(ElggUser $user, array $extra_params) {
	$to_return = array();

	// Make sure the variables have the types we need
	if(!$user instance ElggUser || !is_array($extra_params)) {
		return $to_return;
	}

	// Amazing code which updates the array and returns a standard type

	return $to_return;
}

What happens now if you pass in NULL or the wrong variable type the script will return an empty array. This way your code will not break down because of unexpected values being returned. You know you will always get an array back, now you just check if the array is empty or not.

PHP is not your standards dad's Object Oriented Programming language. If you want to use PHP like C# or Java (etc.) then you will have to work for it.

Frankly you should be checking what parameters are getting passed into your variables. You can never assume the data which will come in is safe. You might be the only one using the function now but another developer might come in and use it to handle direct user inputs.

Never trust any variable, check all your parameters and fuck Elgg.

You filthy comment whore, you love it don't you?
Lets not be forgetting to +1 it now... I am tracking your IP...