PHP The Ultimate Getter

When doing OOP (object oriented programming) in any language it’s proper form to have your variables be private and provide get functions for each of them. In languages like C# this is really easy and convenient to do because C# makes it easy to do. PHP was not created to use OOP style methods and it isn’t quite as simple to do. So, I’ve created this function make life a lot easier for myself.

Let’s make up a fake class to demo this with, here’s the basics of our class:

class Pickles{
    private $brand = '';
    private $is_crunchy = true;
    private $are_whole = false;
}

Okay, so that’s all we are concerned about in the class for this post. Now, to make some getters for that class we would have to do something like this:

function getBrand(){
    return $this->brand;
}
function getIsCrunchy(){
    return $this->is_crunchy;
}
function getAreWhole(){
    return $this->are_whole;
}

Now, I did do one thing there that maybe you don’t like to do and that is switch all my get functions to using a camelCased style. If you like underscores, yours may look a bit different. But that’s my method of choice. Now, we’ve already had to create 3 functions that simply return us the values of our class members. We’d also have to make 3 more for the setters as well, but I’m not going to get into those right now.

Thinking about a class with 20 members already gives me a headache, so simplification is required in my opinion. This is where my ‘ultimate getter’ function comes into play. It allows you to get the value of any class member in any class and even override it if required. First let’s put the whole thing here, then look at the pieces:

//get the value of a class member
function getValue($name)
{
	//piece together a function in the form of
	//getVariable (where variable is the name of the
	//variable you are getting with a capital first letter
	$getFunc = 'get'.ucfirst($name);	
	//check for a specific get function for this variable
	//and return it if found
	if (method_exists($this, $getFunc))
	{
		return $this->$getFunc();
	}
	//make sure the variable exists and then return it's value
	else if (isset($this->$name))
	{
		return $this->$name;
	}
	//$name doesn't exist in object
	else
	{
		return null;
	}
}

First off, notice it only requires you to pass it one thing, and that is the member name that you want. So if we want to find out the brand of our pickle, we just pass it ‘brand’. Easy enough.

Next, it checks for an override. What it does is take the member you passed it and check to see if there is a function called ‘get$name’ in the current class. This means that if you had a special way of returning the brand you could simply create another function called ‘getBrand’ and when you call ‘getValue(“brand”)’ it will return to you the results of ‘getBrand()’.

This part definitely relies all on how you name your functions though. Because if you called ‘getValue(“is_crunchy”)’, even if you have the function ‘getIsCrunchy’ defined, as is it won’t be found. Instead, this function will try to find ‘getIs_crunchy’. This can of course easily be fixed by doing strreplace in here, but I don’t have it. So that’s up to you.

It can also be avoided by called ‘getValue(‘IsCrunchy’)’ if you already know that you have that function created where it should be, but I don’t think this is the best method either. I would go with the strreplace. In fact, I think next I’ll go write a function that takes underscore separated words and returns them camelCased… (/rant).

Anyways, after looking for a (and not finding) a special get function, it checks to make sure the passed $name is a member of the object. If so, it simply returns the value of it.

If all tests failed, ‘null’ is returned to let you know that the member doesn’t exist. Null has been chosen over ‘false’ because ‘false’ may be the value of your member and I didn’t want those to get mixed up with each other.

That’s it! Pretty simple, but could save a lot of time from writing get functions for every member in every class. If you aren’t yet using gets/sets you should start! It’s a very good way of keeping your data safe. But you can read more about that elsewhere, I need to go make my camelCasing function now.

• Luke Stebner •




---

Comment

 
---