Monthly Archive for January, 2008

Page 2 of 2

SPL_Types in php and strong typing

A strong typed programming language is a programming language where the datatypes of values have restrictions on them. The restrictions apply for what datatypes can be stored in variables, whether or not you can operate on those variables and more. To have an overview of a good definition of a strong typed language, see Wikipedia.

By default, PHP isn’t strongly typed. PHP variables and class properties can hold every datatype you wish for, and while doing operations on them, PHP internally uses implicit type casting to convert the one type to another. For example, you could do the following:

1
2
3
4
5
$stringVal = "5";
$intVal = 3;
echo $stringVal + $intVal;
 
# outputs 8

The first variable is initialized as a string, the second variable is an int. In any strong typed language, this would probably throw an error. PHP on the other hand, internally evaluates the operation and typecasts the string value to an int. What happens internally is the value is given to the C function convert_to_double(val);, which then makes the add operation possible and returning the int value 8.

This approach is flexible and definitely has it’s benefits, but sometimes you do want a strongly typed language. Luckily there is a PECL extension just doing that: SPL_Types. The working of this is best illustrated with an example:

1
2
3
4
5
6
7
$int = new SplInt(3);
$float = new SplBool(3.53);
try {
    $int = 'string';
} catch (UnexpectedValueException $e) {
    echo $e->getMessage();
}

As expected, this throws an Exception, because you tried to implicitly convert a integer to a string datatype. Similar SPL Classes exist for other datatypes too:

$int = new SplInt();
$float = new SplFloat();
$bool = new SplBool();
$enum = new SplEnum();

It’s available as a PECL extension, so the usual ‘pecl install SPL_Types‘ should do. It doesn’t make PHP a fully strong-typed language, but the SPL_Types extension isn’t meant to do so. It’s nice to have typing like this when you need it. To have a last example that might be of practical interest:

1
2
$input = $_GET['productCode']; # Unsafe way
$typeSafe = new SplInt($_GET['productCode']); # Type-safe way

Now, if anyone (maybe another developer) wants to change the input you receive from the $_GET['productCode'] key, if they try to do this:
$input = "myOwnProduct"; this will be perfectly valid, but not wanted. If you would do the same with our $typeSafe variable, an exception would be raised, telling you that the implicit typecast is not allowed, and thus improving security.

New-years resolutions for 2008

Albeit a bit too late, here are the plans for this year:

Become Zend Certified
I’ve been wanting to do this for some time now, and the first step has been taken to actually get the certification, so this would be a short-term plan (withing the next 3 months or so). I don’t really need this for my current work but it’s a nice recognition and about the only official certificate that indicates a certain level of PHP knowledge.

Learn more about one or more enterprise PHP frameworks
I’ve had some experience with the Zend Framework and recently Symfony, and I believe there is a definite advantage in using such a professional framework, asides from being able to contribute back to the community in various ways. As I’ve had the opportunity to use Symfony in a current project I’m working on, that will probably be the first framework that I’ll want to dig into a little deeper. Good and thorough knowledge of a framework is always interesting, for multiple reasons.

Do some sports
Lately the most exercise I have is walking from and to my car. Approximately 2 years ago I frequently visited the gym and that’s a habit I’d like to pick up again. Lose weight, exercise, probably one of the most heard new-years resolutions.

Attend at least two PHP, OSS or Linux conferences this year
Actually, this is more or less taken care of. In february you can find me at Fosdem in Brussels and in november this year I’ll be happy to return to the International PHP Conference in Frankfurt (for which I received a free full-conference entrance pass!). Any other interesting conference is of course welcome (the Dutch PHP conference looks cool and is in my native language).

Settle in my new appartment
Probably around next month I’ll move in my new apartment. This will be a good opportunity to buy new furniture and have a nice place to stay. The whole move will probably take some time, and this is also planned for this year.

PECL and php extensions
Since 2 months I like to do some work on php extensions in my spare time. I As I’ve just started in creating php extensions, it’s not sure whether I’ll get far enough to actually contribute something useful. It’s definitely more difficult if you don’t have a decent C background, but I get the basics and you’ll learn a lot of the inner workings of PHP and the Zend core engine. At least I now know what a zval is :)

Let’s get back at this post at the end of 2008, and give myself a brief self-evalutation :)
But for the time coming, this is my personal todo list that I’d like to work on.

__callStatic in PHP 5.3

This is cool (and a feature I’ve been missing in past projects): being able to implement a __call method for static class methods.
Like this:

1
2
3
4
5
6
7
class Foo
{
	public static function __callStatic($method, $args) {
		echo "Method $method has been called!" .PHP_EOL;
		echo "With following arguments: " .implode(", ", $args) .PHP_EOL;
	}
}

So you can call

Foo::anyMethod('with', 'any', 'arguments');

and have a working static class method.

In this case, the method outputs

Method anyMethod has been called!
With following arguments: with, any, arguments

This is already implemented in the PHP 5.3 and PHP 6 development versions.
For more information on what __call (and the future __callStatic) magic methods do, see php.net Method overloading

PHP4 End of life, finally

Together with the last and final release of PHP 4.4.8, PHP4 is finally end of life. To quote the php.net news item:

The PHP development team would like to announce the immediate availability of PHP 4.4.8. It continues to improve the security and the stability of the 4.4 branch and all users are strongly encouraged to upgrade to it as soon as possible. This release wraps up all the outstanding patches for the PHP 4.4 series, and is therefore the last normal PHP 4.4 release. If necessary, releases to address security issues could be made until 2008-08-08.

I’ll post this release notice an effort to spread the word as much as possible: if you’re still developing projects in PHP4, you should seriously consider making the switch to PHP5. To have a quick look of what OOP goodies you can get in PHP5 (and thus make your programming life a lot easier), have a look here.

On the time of writing, there’s about 32 days left for PHP4 to be totally end-of-life. That’s 32 days to clear your php4 sins and start porting your applications to PHP5! To get to know more about that, see the GoPHP5 initiative. Or click the logo on the right menu. You won’t be sorry.

About attaching and detaching Xen domU mounts

If you ever need to replace a mount of a Xen domU guest without rebooting the server, you can’t just remount it and be done. In my case, I needed to increase the size of the swap file a certain DomU was using.

Independent from mounting or unmounting a partition inside the DomU, Xen saves its virtual device information inside a logical structure in an information storage called the XenStore. So to change partition information without rebooting the image, you need to inform Xen that you’ll be changing things, and want to detach the virtual device (in our case, a swap file) from the DomU. This is the way:

In the domU that needs to be changed, turn off the swap space:
# swapoff /dev/sda2

Then, in Dom0, list the domains for an overview:
xen1:~# xm list
Name ID Mem(MiB) VCPUs State Time(s)
myVM 17 2000 1 -b---- 134487.4

List the virtual block devices that are coupled to the specified domain:
xen1:~# xm block-list 17
Vdev BE handle state evt-ch ring-ref BE-path
2049 0 0 4 6 8 /local/domain/0/backend/vbd/17/2049
2050 0 0 4 7 9 /local/domain/0/backend/vbd/17/2050

Read what vdev is coupled to what device name inside the domU:
xen1:~# xenstore-read /local/domain/0/backend/vbd/17/2050/dev
sda2

Detach the swap partition:
xen1:~# xm block-detach 17 2050

Create the new swap space (to create a 1G swap file):
xen1:~# dd if=/dev/zero of=/xen/swap/myVM.swap bs=1024k count=100

Attach the new swap partition back to the domain:
xen1:~# xm block-attach 17 file:/xen/swap/myVM.swap sda2 w

Then, with this in place, go back to the domU console, create the swap filesystem and activate it:
# mkswap /dev/sda2
# swapon /dev/sda2

That’s about it for updating a mount point inside the DomU. This can be done for every block device that a Xen DomU may use, as long as it isn’t system-critical, such as the root filesystem).

This procedure also makes use of some lesser known commands such as xm block-attach, xm block-detach and xenstore-read.
Although there isn’t much documentation about these commands, they are worth checking out in case you would need them.