Convert a PHP object into an array

SOAP loves to throw objects of objects of objects back at you when you make a call. There isn’t anything terribly wrong with that, but it makes it a bit hard to loop through the object’s properties using a foreach loop. get_object_vars can convert an object to an array, but leaves any objects contained within the object as objects, which makes it sort of pointless by itself. So here is a function that converts ALL of the objects in an object to arrays.

function conv_obj($Data){
	if(!is_object($Data) && !is_array($Data)) return $Data;

	if(is_object($Data)) $Data = get_object_vars($Data);

	return array_map('conv_obj', $Data);
}

First, we pass the object into the function. If the object isn’t actually an object or an array, then we return it as-is.

If it is an object, then we use get_object_vars to convert the object’s properties into an array.

Last, we use array_map to put all of the elements of $Data (the passed in object/array) through the same conv_obj function that we are already using so that all of the elements in the entire object/array are converted into arrays.

Nifty, eh?

PS- I didn’t write this code, which should be obvious by the lack of curly braces in the if statements.

For use in a class

function ConvertObjectToArray($Data){
	if(!is_object($Data) && !is_array($Data)){
		return $Data;
	}

	if(is_object($Data)){
		$Data = get_object_vars($Data);
	}

	return array_map(array($this,'ConvertObjectToArray'), $Data);
}
Read More

Are you paying too much for your code repositories?

I consider myself a fairly active web developer.  I have around 50 websites and projects that I actively manage, and each of these has their own git repository.

To ensure I have easy access to all my repos, I’ve been storing them in my Dropbox. The upside to this is that all my computers have instant access to the latest repo. The downside is that I don’t have any fancy tools to browse change logs and it is relatively easy to corrupt a repo.

So I started moving things to GitHub, a leader in git repository hosting. GitHub provides a very nice interface to give you access to your repositories, both public and private, and makes it easy to give others access as needed. I quickly realized, however, that I couldn’t afford it. With 50 repos I would need a Gold business plan that costs $100/month. Yikes!

My friend Joey pointed me towards Repository Hosting. They charge $6/month for unlimited repositories up to 2 GB total, and an additional $1/month for each additional GB. Not bad. For my needs that would be about $12/month, so over $1000 savings per year. Sadly, they aren’t nearly as polished as GitHub.

I decided to do some searching for other alternatives and found Bitbucket. They charge $0/month for unlimited repositories and unlimited storage space. Wow! They do start to charge if you add more than 5 collaborators to your projects, but this fee can easily be bypassed by adding a .edu email address to your account. Even better, their feature set is very similar to GitHub and I found Bitbucket’s interface to be a bit more intuitive than GitHub’s.

I’m always a little leery of free services. However, Bitbucket is owned by Atlassian, which is an awesome company that makes all sorts of useful development tools. They are one of my favorite tech companies because most of their products have a starter package that offers enough functionality for a small business but is as little as $10. For example, a 5 user version of Crucible (a code review tool that I highly recommend) is only $10. It is easy to get hooked on their tools when they make it so affordable to get started. This company has plenty of customers and money and isn’t going away anytime soon.

So are you paying too much for your git code repositories? If you are paying anything at all then you are probably paying too much.

Read More