Read-only object variables in php using magic methods

January 22, 2009

You can create read-only object variables by using the “private” keyword and the __get() and __set() magic methods like this:

class classWithReadOnlyVar
    private $readOnlyVar;

    public function __get($varName)
        return $this->$varName;

    public function __set($varName,$varValue)

So now classWithReadOnlyVar::readOnlyVar is only settable from inside the class, but you can read it from anywhere. For example, if you add the above constructor to the code above:

    public function __construct()
        $this->readOnlyVar = "foo";

Then the following code:

$test = new classWithReadOnlyVar();
echo $test->readOnlyVar;

$test->readOnlyVar = "bar";
echo $test->readOnlyVar;

has this output:


Create a select option using javascript with the euro symbol in the text

December 5, 2008

This didn’t work:

new Option("€"+value,value);

Neither did this:

new Option("&euro;"+value,value);

But this did:

new Option("\u20AC"+value,value);

Zend_Db_Adapter::update() returns number of rows in which values have been changed

December 5, 2008

… and not the number of rows that match the conditions.

Just so you know. Caught me out for a while there – I was using the return value from update() to check that certain values (including a timestamp) were set correctly while at the same time updating the timestamp, but if the method got called twice in a second I’d get zero as a return value the 2nd time because the timestamp was the same and so no values were being changed.

Show / hide table row in Firefox versus IE

November 28, 2008

Hiding a table row is fine, just set document.getElementById(‘theRow’).style.display = ‘none’

When you’re trying to show it though, it can get confusing – for it to display properly, you’ve to set display=’table-row’ for Firefox and display=’block’ for IE.

The solution? Set display=” and the display property for the element gets set to its default (‘table-row’ for FF and ‘block’ for Explorer).

Email and date validation without regular expressions

November 28, 2008

I just discovered filter_var() –  very handy for validating form input, I’ve switched over from validating emails with regular expressions and I use this instead now:


… so now I don’t have to worry me getting the regexp wrong and rejecting emails when I shouldn’t. Nice. I used to validate date using regular expressions too, changed that too recently to use checkdate() (another new discovery, even though it’s been around since php4).

If only javascript had better built-in functionality for validating this stuff on the client side too I’d be laughing

Safari javascript validation problem

November 19, 2008

This seems to becoming more of a javascript blog lately …

Anyway, I was editing some javascript validation code that was written by someone else, and it wouldn’t work in Safari although it was working fine in FF and IE. The quirky thing about the validation code was it was making sure ALL form variables except specifically-excluded ones had a value (I normally do it the other way around, check specific variables have a value) and, it turned out, the problem with Safari was unchecked radio buttons get submitted as blank values – in IE and FF they just don’t get sent in the http post request. Added those to the list of variables excluded and it worked

Realex payments error 505 “You cannot use this service from there”

September 2, 2008

Did a booking engine for a hotel lately, and I wanted to record the booking details before redirecting the user to the payment processor’s site (realex in this case), so the final form was submitted to my own application, then I used php’s curl functions to get the payment page from realex.

In the response from realex’s server to the http request sent by curl I was getting an error with error code 505 and the message “You cannot use this service from there”. Before payment goes live for an account, realex asks for the url that you’ll be redirecting to their system from (the “incoming url”, they call it) and then they reject requests from any other “incoming url”. What this means, it turns out, is they check the “Referer” header in your incoming http request and make sure it matches the “incoming url” you’ve supplied to them. The request I was sending via curl wasn’t sending a “Referer” header, hence the error. Solved the problem like this:

$ch = curl_init(PAYMENT_PAGE_URL);
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Referer: <myurl>"));

Fast server-side rejection of large image uploads using $_FILES

August 28, 2008

Discovered today you can report to a user if the file(s) he/she is uploading is too large without having to wait for the file to finish uploading by checking $_FILES – the $_FILES array for each form input of type “file” has an element called “error” which returns an error code without actually uploading the file if the file is larger than than upload_max_filesize in php.ini or $_POST[“MAX_FILE_SIZE”]. It can do this because a “Content-length” http header is sent to the server first, and the file itself is then sent in the body of the http request.

Here’s a very simple example.

The form:

<form enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
    <input type="file" name="image" />
    <input type="submit">

The php:

if ($_FILES["image"]["error"] == UPLOAD_ERR_FORM_SIZE)
    echo "file too big!";

Note that you shouldn’t just use MAX_FILE_SIZE as I’ve done above, you also need to set upload_max_filesize appropriately in php.ini

setAttribute not working for onclick in IE

August 8, 2008

You can’t use javascript setAttribute() to set an onclick event on a link in Internet Explorer, I’ve discovered. You have to use <element>.onclick = <something> instead.

See  here and here

Changing style.display using javascript not working in IE

August 8, 2008

I was using a fairly simple js script to show/hide a div. Worked fine in firefox, didn’t work at all in IE (and had the unexpected side-effect of hiding the link I wanted to click on to toggle the show/hide). The problem code is below:

<a href="#specialConditions" name="specialConditions"
onclick="toggleDisplay('specialConditions');"> Special conds</a>

<div id="specialConditions" style="display: none;">

The function toggleDisplay() just sets style.display=’none’ if it’s equal to ‘block’ and vice-versa.

The problem turned out to be the ‘name=”specialConditions”‘ in the link – IE seems to confuse names and IDs in getElementById(), so document.getElementById(‘specialConditions’) was returning the link instead of the div.

So I changed the value of “name” in the link and it worked fine:

<a href="#specialConditionsAnchor" name="specialConditionsAnchor"
onclick="toggleDisplay('specialConditions');"> Special conds</a>

<div id="specialConditions" style="display: none;">