Christopher Shennan’s Blog

A day in the life of…

Browsing Posts tagged PHP

Over the last few months I have been working with Symfony and have got used to passing values to methods via an options array but I have recently had to pick up an older project in which the values are passed to the methods by individual parameters.

This has given me a good opportunity to evaluate both techniques and determine my personal preference for working.

The more traditional technique is to pass values to methods using individual parameters as in the example below:-

public function someMethod($name, $age = null)
{
  // do something
}

The benefit I have found with this method is that you can clearly identify what needs to be passed to the method, however, if you need to change the method to include an additional parameter it means that you have to update every call to this method so that every parameter is specified.
continue reading…

This has been updated to work with Symfony 1.3/1.4 – Please go to the updated article: Raw SQL from Doctrine Query Object – Revised

As my use of Symfony is increasing I find myself frustrated that there doesn’t appear to be an easy way to get the raw SQL from a doctrine query object so I can simply output it and paste it into phpMyAdmin to debug problems with the SQL. I may be wrong about this but as yet I have not found a built in function to perform this operation.

Sure I am able to output the query with the “?” indicating where the necessary values are to go and I can also easily output the array of query parameters so I can substitute these manually but this takes time and it would be much much simpler and quicker to just output the whole query with the substitution already done so it’s a simply copy and paste of the raw SQL from the doctrine query object straight into phpMyAdmin.

I have wrote a quick function to do this and I had meant to post this for a while but I just have not had the chance to get round to it until now.
continue reading…

One simple thing I keep having to do over and over again is to display the name of the month for a given date.  Normally I have opted to use the first solution that comes to mind which is creating an array with all the month names in it and then when I want to retrieve the month name I just use

$monthName = $aMonth[$monthNum];

However, when I wanted to use PHP to display the month name this time I wanted to see if there was a better solution.

The one I came across was posted on The Code Cookbook and was a simple 1 line statement which was great… simpler, neater and far more elegant than my usual method and it looked like

$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));

Normally I would have been happy with this, however was needing to deal with a month drop down box which didn’t run from January to December but instead ran from November to April but this worked out to be rather simple too.

continue reading…