January 30th, 2007

Using variable variables

Posted by Jeremy2 in PHP

This is just a really quick one, as it is fairly simple, but I have never seen anybody else do this for objects / functions in PHP so I wanted to write this down before I forgot it. It took me a while to figure out how to do variable variables for objects, but I never realized that you can also call functions and create new objects using variables. For those of you who do not know what a variable variable is, here is a quick demonstration:


< ?php
$test = "Hello!";
$some_var = 'test';
echo ${$test};
?>

The brackets around the variable name are optional. What this does, is it takes any string (whether in variable form or a plain string) and it treats it just like it’s a variable. You can also do string concatenation in the variable name like so:

$some_test = 'Hello!';
$some_var = 'test';
echo ${'some_'.$some_var};

In this case, the brackets are not optional. What I did not realize is that you can also have variable functions and variable classes, like so:


class some_class {/*empty class*/}
class some_other_class {/*empty class*/}
function some_func($a) {
echo $a;
}
$the_class_name = 'some_class';
//Creates a new instance of some_class
$new_obj = new $the_class_name;
$the_func_name = 'some_func';
$the_func_name('Hello!');

To use variable variables in objects, this is how you do it:


$obj = (object) NULL;
$obj->some_var = 'Hello!';
$var_name = 'some_var';
echo ($obj->$var_name);

To access functions using a variable, here is code:


function print_test($a) {
print $a;
}

$func_name = 'print_test';
$func_name('Hello!');

You can use a similar syntax to access functions in objects as you can to access the variables: just put the variable that contains the string with the parentheses around the arguments like this:


$obj->$func_name('Some argument');

(more…)

January 25th, 2007

Googling oneself

Posted by Jeremy2 in General Musings / Rants

Yes, I admit it: I Google my name. Why do I do it? For fun. I enjoy seeing my sites at the top of Google for my name, and I am also interested in seeing what the other Jeremy Nicolls are doing out there. Yes, there are multiple. From what I can tell there are at least four other Jeremy Nicolls in this nation and at least one other that shares my middle name (I’m not going to give out all of my information over the Internet). It’s also interesting to come across old posts that I made a while back. I do a search for “Jeremy2″ as well, but there are a lot of things that are not related to me under this pseudonym. The whole story behind the name Jeremy2 is that Jeremy was one of the most popular names to give babies when I was born. Everywhere I have gone there seems to have always been one of at least two Jeremys. It’s a play off of the phrase, “I’m Jeremy, too.”

Maybe its because I spend way too much time at the computer screen and need to feel important - I guess that’s up for debate. I also contemplate linking to some article stating that Jeremy Nicoll was just named to the board of directors for some big company just for laughs, but I don’t to say something untrue that could potentially go over the world - even if that possibility is remote. For as much as some people get made fun of for doing it when they admit it, I don’t think there is too much or a problem with Googling oneself. I did hear Jay Leno make fun of some celebrity who claimed he was much more popular than another because there were more sites about the first celebrity. That I can understand: “I’m popular because people write about me on the Internet,” sounds kind of like a playground taunt to me. Well so-and-so likes me more than you! Oh well. I’m rather morbidly interested to see if anyone has written anything negative about me. I doubt it is nothing that I have not read yet (mostly it’s in forums and not blogs), but for some reason I get a kick out of it. Perhaps I will delve past the first couple of pages next time.

January 24th, 2007

Table min/max width bugs

Posted by Jeremy2 in Browser Bugs

I have tested this in IE7, Opera, Firefox, and Safari (latest to date versions) as well as WebKit. What I have found is that for some stupid reason, IE7 and Safari do not respect min and max widths on tables. Unfortunately due to a couple of limitations in CSS, and also because IE 6 with its garbage CSS support is unfortunately still used in large numbers, I am forced to use tables for layout on occasion. I hate it, but I do what works. I can’t always work on principle alone :). Anyway, if you find yourself needing a fix for this dilemma, it’s pretty easy: enclose the table in a DIV, which all the latest browsers support these properties on.

So:


<div style=”min-width: 45em; max-width: 55em; width: 50%; position: relative”>
<table>
<tr>
<td>Table Content</td>
</tr>
</table>
</div>

I have included the minimum amount of code needed in order to get this to work properly in all latest version browsers. To get IE 6 to behave like it supports min-width, I suggest you do a Google search on that one. I try to make my designs so that overall it functions better with these attributes, but works OK if the browser does not display it correctly.