Blog
2007 12 posts (3)
Ordering: Ascending Descending
1. Strong typing in PHP
2007-12-28 18:20:55 by Martynas Jusevičius
Some time ago David Coallier posted a short example on how to use SPL_Types library to achieve strong typing in PHP. For example, it allows you to define explicit types using classes:
$int = new SplInt(3);
$float = new SplFloat(3.1412);Too bad that more examples or documentation are missing, but David mentions he has a tutorial in the works.
An external library is needed since PHP uses weak typing by default. There are some wishes however to have strong typing in PHP 6. It's arguable if PHP would benefit from it (and it would be a major shift in the design of the whole language), but if you prefer making your PHP code more Java-like, you might find SPL_Types useful.
2. Reading Excel files with PHP
2007-12-14 14:39:17 by Martynas Jusevičius
There is a nice tool for reading Microsoft Excel files called PHPExcelReader. It works with .xls files up to Excel version 2003, which are based on the BIFF format (later versions use OOXML). It is written in native PHP and does not require any third-party libraries or the MS Office package.
Looping through all cells in a sheet with PHPExcelReader is as simple as this:
require_once 'Excel/reader.php';
$reader = new Spreadsheet_Excel_Reader();
$reader->setOutputEncoding("UTF-8");
$reader->read("test.xls");
for ($i = 1; $i <= $reader->sheets[0]["numRows"]; $i++)
{
for ($j = 1; $j <= $reader->sheets[0]["numCols"]; $j++)
{
print "\"".$reader->sheets[0]["cells"][$i][$j]."\",";
}
echo "\n";
}3. PHP 5 features: Class autoloading
2007-12-05 11:46:11 by Martynas Jusevičius
Continuing the series about useful features in PHP 5, another overlooked one is class autoloading. From the manual:
Many developers writing object-oriented applications create one PHP source file per-class definition. One of the biggest annoyances is having to write a long list of needed includes at the beginning of each script (one for each class).
In PHP 5, this is no longer necessary. You may define an __autoload function which is automatically called in case you are trying to use a class which hasn't been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.
Basically, you get a class name as a parameter in your __autoload() function, and using it you have to figure out the path to the actual class file and include it.
If all of the classes are at known paths in known folders, you might be able to find the class file path using simple string concatenation. However, if the folder structure is more complex, a convenient solution is to have a class name/class file path map (associative array), whether hand-made or generated:
function __autoload($classname)
{
$classes = array(
"NotFoundView" => APP_VIEW_DIR."notFound/NotFoundView.class.php",
"ErrorView" => APP_VIEW_DIR."error/ErrorView.class.php"
// and so on
);
if (array_key_exists($classname, $classes)) require($classes[$classname]);
// using name as key, get the path and include the file
}There can be several autoloading functions named differently, but then they have to be registered explicitly using spl_autoload_register(). Class methods can also be used.
These hacks are not very clean, but they help to bring PHP one little step closer to object- and library-oriented approach. For example, a library (such as Propel or DIY Framework) can be implemented with its own class autoloading, and including a single file that handles it will be enough to load the whole library.
Propel 1.3 uses this feature already, and we are implementing it in the new release of the DIY Framework. The framework loader class implements autoload() method, and at the end of the file it is registered using spl_autoload_register(array("DIYFrameworkLoader", "autoload")). Including the loader class effectively loads the whole framework.
Ordering: Ascending Descending
