When should I use ‘self’ over ‘$this’?

Short Answer Use $this to refer to the current object. Use self to refer to the current class. In other words, use $this->member for non-static members, use self::$member for static members. Full Answer Here is an example of correct usage of $this and self for non-static and static member variables: <?php class X { private … Read more

Alternative for PHP_excel

For Writing Excel PEAR’s PHP_Excel_Writer (xls only) php_writeexcel from Bettina Attack (xls only) XLS File Generator commercial and xls only Excel Writer for PHP from Sourceforge (spreadsheetML only) Ilia Alshanetsky’s Excel extension now on github (xls and xlsx, and requires commercial libXL component) PHP’s COM extension (requires a COM enabled spreadsheet program such as MS … Read more

PHP + MySQL transactions examples

The idea I generally use when working with transactions looks like this (semi-pseudo-code): try { // First of all, let’s begin a transaction $db->beginTransaction(); // A set of queries; if one fails, an exception should be thrown $db->query(‘first query’); $db->query(‘second query’); $db->query(‘third query’); // If we arrive here, it means that no exception was thrown … Read more

Multiple file upload in php

I know this is an old post but some further explanation might be useful for someone trying to upload multiple files… Here is what you need to do: Input name must be be defined as an array i.e. name=”inputName[]” Input element must have multiple=”multiple” or just multiple In your PHP file use the syntax “$_FILES[‘inputName’][‘param’][index]” … Read more

Why check both isset() and !empty()

This is completely redundant. empty is more or less shorthand for !isset($foo) || !$foo, and !empty is analogous to isset($foo) && $foo. I.e. empty does the reverse thing of isset plus an additional check for the truthiness of a value. Or in other words, empty is the same as !$foo, but doesn’t throw warnings if … Read more