Making isset Recursive

At work I was told to add in the magic method __isset() (which is technically done by overloading the function…). Essentially it lets you call isset() on private and protected variables in your classes:

 
/**  As of PHP 5.1.0  */
 
class MyClass{
 
private $private;
 
public $public;
 
protected $protected;
 
public function __isset($name) {
        echo "Is '$name' set? ";
        return isset($this->name);
    }
 
}
 
$myClass = new MyClass();
 
echo isset($myClass->private); #Is 'private' set? false
 
echo isset($myClass->protected); #Is 'protected' set? false
 
echo isset($myClass->public); #false

This function will be called whenever the variable is unreachable by a normal isset call (private and protected variables)

Nice and easy if you know exactly what you want. Unfortunately sometimes people have the habit of adding underscores to the start of their variables, but not always. So to make this work, I decided it would be a good idea to make it check to see if the variable was there, or if maybe there should have been an underscore at the begining. So it became:

 
/**  As of PHP 5.1.0  */
 
class MyClass{
 
private $_private;
 
public $public;
 
protected $protected;
 
public function __isset($name) {
    if(isset($this->$name)){
        return true;
    }
 
    $name = "_".$name;
        return isset($this->$name);
    }
 
}
 
$myClass = new MyClass();
 
echo isset($myClass->_private); #Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 134136 bytes) in isset_r.php on line 20

Curious, so I changed it a bit to see what was actually happening in there…

__private___private____private_____private______private_______private________private_________private

__________private___________private____________private_____________private______________private___

____________private________________private_________________private__________________private______

_____________private____________________private_____________________private______________________

private_______________________private________________________private_________________________priv

ate__________________________private___________________________private__________________________

__private_____________________________private______________________________private_______________

________________private________________________________private_________________________________

private__________________________________private___________________________________private______

______________________________private_____________________________________private______________

________________________private_______________________________________private__________________

______________________private_________________________________________private__________________

________________________private___________________________________________private______________

______________________________private_____________________________________________private______

________________________________________private_______________________________________________

private________________________________________________private

Fun stuf… the lesson is: name things a standard way and you are good to go, if you don’t it will screw things up royally (but it might make a pretty design).

A look at PostgreSQL

Almost all last week at work I was working on adding Postgres to the database interfacing part of our php framework. The more I worked on it and did research to figure out why things were the way that they were and why certain things didn’t work the same way as I thought, because of my limited knowledge of databases other than MySQL and a bit of Oracle, they should, the more that I liked this database backend (side note: why doesn’t chrome have an add to dictionary option when right click on something you ‘misspelled’?). It is not that one is definitively better than the other, it is just a different way of tackling the same problems (well… in my case they are pretty much the same problems…).

For the most part, simple queries that you would write are the exact same:

SELECT * FROM my_table WHERE my_column = 'something'

This will of course return all of the things in the database where my_column = ’something’, nice and simple.

Problems sometimes arise however when you want to do more complex things. For example if you want to delete everything from two tables where one column is the same as the other:

In MySQL, easy:

DELETE table1.*, table2.* FROM table1, table2 WHERE table1.column1 = table2.column1

In Postgres, it is easy as well, but different:

(Downloading and installing Postgres so I don’t mess this up… that would be embarrassing…)

CREATE TEMPORARY TABLE mytmptable(column1 text);
INSERT INTO mytemptable(SELECT table1.column1 FROM table1, table2 WHERE table1.column1 = table2.column1);
DELETE FROM table1 WHERE table1.column1 IN (SELECT * FROM mytemptable);
DELETE FROM table2 WHERE table2.column1 IN (SELECT * FROM mytemptable);

You could do the delete in a similar way to that in MySQL, but you probably wouldn’t because the other way is simpler. It is more dangerous though because of the possibility to delete everything in your tables while you are testing by accident if you miswrite the query.

Fortunately not everything is more complicated in Postgres, for example you can do:

DELETE FROM table1 WHERE column1 IN (SELECT column1 FROM table2 WHERE column1 LIKE 'some text' LIMIT 10 OFFSET 2)

and it will have no trouble deleting things from your tables. In MySQL you cannot do this because of the way that MySQL queries the tables when it uses LIMIT and OFFSET (apparently). What you have to do is this:

CREATE TEMPORARY TABLE mytemptable(column1 text);
INSERT INTO mytemptable(SELECT column1 FROM table2 WHERE column1 LIKE 'some text' LIMIT 10 OFFSET 2);
DELETE FROM table1 WHERE column1 IN (SELECT column1 FROM mytemptable)

This makes postgres a more attractive option to me… if only it supported

CREATE TABLE IF NOT EXISTS ...

Adding hints

Sometimes it is not totally obvious what you are looking for the user to enter into a text box.

Take this one here:

It is not obvious what we are looking for in this form, By email might is probably an email address. In person is probably an address. Online? That could also be an email address, a skype name, a msn messenger account name, a website…. you get the idea. Other, well, that could be anything, but what is that second box for? To be fair, the form has values in it telling what should be done, I removed those for the sake of the example. That is indeed one way of doing it, but that means that someone has to select all of the text and delete it when they get to that box. I don’t know about you, but I find it a little annoying when I have to do that… So, I started looking for a solution, and I found one that uses jQuery (my javascript library of choice). I could have written it myself of course, its not that hard really:

if(form.input is empty) {
   form.intpu.value = form.input.text
}
if(form.input is clicked){
   form.input.value = ""
}
if(form is unclicked and value = ""){
   form.input.value = form.input.text
}
if(when form is submitted and form.input.value == form.input.text){
   form.input.value == ""
}

I decided to modify that slightly, so that if the form was filled with the value found in text it would be lighter than normal, so:

if(form.input is empty) {
   form.intput.value = form.input.text
   form.css("color", "#999")
}
if(form.input is clicked){
   form.input.value = ""
   form.css("color", "#000")
}
if(form is unclicked and value = ""){
   form.input.value = form.input.text
   form.css("color", "#999")
}
if(when form is submitted and form.input.value == form.input.text){
   form.input.value == ""
}

Here is the final result:

Oh ya, a bonus is that when they hover over it and have javascript turned off it will show the hint as well, and it is screen reader friendly! Accessability is good!

If anyone wants the code I can post it.

Updated Wordpress

I finally decided to update wordpress and the plugins that I use to go with it. The update seemes to have gone well, and I believe that my site is still working fine. My theme still seems to be working too… though I think I need to figure out the Newer, Current, and Older Post links a bit better because

I really don’t like how they show up at the bottom there when you are cycling through the posts. I also found out that the version of wordpress you are running is found in a meta-tag on your site… Might as well have it written down in plain sight (in the footer maybe? No, the number in my footer has nothing to do with versions, it is the number of times that Wordpress hits the database to generate the page…). I think I am going to hack that part of the code out of Wordpress. The offending bit of code:

<meta content="WordPress 2.6" name="generator"/>

You wouldn’t even need to be clever at all to hack my blog if it were out of date, a lot of the vulnerabilities are made public, a simple search for Wordpress + 2.3 + vulnerabilities would turn up a bunch of hits (and it does):

… about 157,000 for wordpress 2.3 vulnerabilities.

You probably would not get the same amount of results if you used cuil.com instead (burn!).

Ok, I think I am done ranting about this for now.

Time for some learning:

Zinc Oxide is important.

Why implement PHP in Java?

First of all, why not? Everything else can run on the jvm (yes… even c#), so why not php? Well… I certainly don’t see why not… Java is a fairly robust language with a pile of different libraries and (as you saw if you clicked on any of those links) languages that can run on it. It is ideal!

Don’t like php’s random function? Why not use Java’s? Not your cup of tea? Use ruby’s instead, or python’s, or  write one in COBOL if you are really brave.

It also means that you can implement the functionality in Java, the database transactions in Ruby, then write all of the presentation in PHP, no clearer MVC separation than that eh? Admitedly it might get a bit confusing and I may be over exaggerating about the possibility of these implementation’s cross communication abilities… but it would be great if you could (I might test it later…).

http://www.caucho.com/resin-3.0/quercus/index.xtp

Go check it out!