Tag Archive for 'PHP'

Page 3 of 3

PHP London 2008, an overview

I’m just back from PHP London 2008 Conference, where I had a lot of fun and could attend interesting talks. 

After I arrived at about 8:15 in the Inmarsat conference building, I realised I was way too early. Not a problem at all, that gave me the time to have a look around at the different conference rooms and where the catering is located. Always look for the quickest way to grab a cup of coffee :)

The presentation I was looking forward to the most was Ivo Jansch‘s talk about Enterprise PHP. It was a great talk with a very easy to understand analogy to bricks and buildings (maybe Ibuildings? :) ), which made this humorous talk one to enjoy. It does overview the key points of enterprise development very well, which definitely got me (even more) interested in his upcoming book about Enterprise PHP development. Imho, there’s totally not enough ‘awareness’ of using PHP in an enterprise environment, so everything that changes this is a good thing!

Next up were the guys from vBulletin (Scott MacVicar and Mike Sullivan), giving a talk about their experiences while being on the vBulletin team for a couple of years already. For some points, they elaborated on what Ivo just skimmed on at the talk before, but they also gave some interesting insights on what problems and issues you could encounter while developing such a big project.

Of course I’ve been waiting for the Frameworks panel discussion too, having experience with 2 out of 3 discussed frameworks (Zend Framework and Symfony that is), I was looking forward to what items they would bring that made ‘My framework better than yours’. I actually liked the total lack of flaming and instead having a nice overview of where the framework in question shines and what the strengths of it were.

The last talk on the planning was Derick Rethans, presenting a keynote about his experiences with PHP in all those years that PHP has grown from a little personal script to help Rasmus out, to a mature, object-oriented language that can be used in a multitude of environments. Too bad we weren’t able to attend the whole keynote as we had a a Eurostar train to catch. I did already see this presentation at the Internation PHP Conference 2007 , and it was nice to see that parts of the presentation were updated.

In general, this was a very good (but way to short) conference.I definitely had a lot of fun, met various interesting people and enjoyed the presentations and talks that were given.

Dutch PHP Conference 2008

All the cool geeks are going, so you better also be present!
The conference website is released just today, and the dates are known.

Mark your calendars for the 13 and 14th of june, and be part of the one and only Dutch PHP Conference, with an already very impressive speaker line-up! Don’t wait for too long to register for the event, word on the street is that tickets are very quickly sold-out.

The venue will be in Amsterdam, more information on http://www.phpconference.nl.

observe_field in symfony and it’s options

While looking for possible options for the observe_field() helper in symfony, I noticed there wasn’t a lot of documentation around to describe what the possible options are to pass to it. After a bit of searching, I found what I wanted and thought I’d share it in case someone else would need a similar thing:

<tr>
  <th>IP:</th>
  <td>
        < ?php echo object_input_tag($server, 'getMainIp') ?>
        <div id="valid_ip"></div>
        < ?php echo observe_field('main_ip', array(
                'url' => '@lookup_ip',
                'frequency' => 3,
                'update' => 'valid_ip',
                'with' => "'ip='+$('main_ip').value",
                'loading' => "Element.show('indicator')",
                'complete' => "Element.hide('indicator');"
        )) ?>
        <div id="indicator" style="display:none"></div>
  </td>
</tr>

I needed to create an input field for an IP. If you want to change the IP, a request should be made to check if the IP is still available or not.

What this basically does is check if the field 'main_ip' (this is the object_input_tag() field) changes, and it does that every 3 seconds. If it has changed, we go to the url '@lookup_ip' and pass it one parameter, named ‘ip’ with the value of the input field we are observing. During loading the request, we show an indicator that we’re loading in the background, and if the request is complete, the indicator is hidden and the div 'valid_ip' is filled with the output of our action.

Pretty easy if you have a look at the code, but there aren’t much docs around that give examples for this. A good resource for these things is the Symfony JS and Ajax cheat sheet you can find here. (I chose the english version ;))

PHP Constants, the lesser known ones

Often, when looking into people’s code, I find little code fragments that could be designed a bit more elegant. Sometimes the code needs refactoring or sometimes some design patterns needs to be applied.
This time though, I’d like to look at some PHP constants that are lesser known, but can make your code cleaner, less error-prone and more portable.

PHP_EOL:

Returns a line ending (EOL = End Of Line), depending on the operating system you run your code on.

$greeting = 'hello world';
$name = 'felix';
echo $greeting . PHP_EOL . $name;
 
#outputs:
hello world
felix

DIRECTORY_SEPARATOR:

Returns the directory separator for the current operating system. On Linux this is ‘/’, but for example on a mac, this is ‘:’. Using DIRECTORY_SEPARATOR increases portability of your code.

$path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'site.class.php';

PHP_INT_MAX:

Returns the upper bound value an integer can hold for the current operating system. Values will be different on 32-bit or 64-bit systems, so when dealing with large numbers on different hardware systems, take this in consideration.

PHP_OS:

Returns the operating system that PHP is installed on.

if (PHP_OS == 'Linux') {
echo 'yay, linux!';
} else {
echo 'ugh, windows..';
}

PHP_SAPI:

Returns the SAPI that you’re currently using. This is the same as the PHP function php_sapi_name(), but supposedly should work a bit faster, because there is no need for a function call. PHP_SAPI can be interesting if you want to write code that runs on either a website or independently on PHP Commandline (in which case, PHP_SAPI outputs CLI or CGI).

// Check if we're running commandline,
// in which case we can thread our application
if (substr(PHP_SAPI, 0, 3) == 'cli') {
$pid = pcntl_fork();
// ...
}

PHP Abstract podcast about Symfony

 PHPAbstract has a cool podcast up about the symfony framework. This is presented by Stefan Koopmanschap, web application developer at Ibuildings.nl.

His podcast is a brief introduction to symfony, what programming principles it uses, why it can be an advantage for you to deploy your project with symfony and more. A brief and interesting introduction talk to symfony.

The link to the podcast can be found here.

Installing symfony in a subdirectory of your documentroot

I recently needed to set up a Symfony install without a separate virtual host, so as a subdirectory of the (Apache) DocumentRoot directory (like: http://www.example.com/symfony/).

As you may know, symfony appreciates it when it has a reserved virtual host for itself, but also provides ways to just install it in a separate directory. The following steps need to be taken:

1. Change the apps/<yourapp>/config/settings.yml file and set relative_url_root


all:
  .setttings:
    relative_url_root: /subdir

2. Go to your front controller’s .htaccess file (usually in web/) and modify the RewriteBase directive


RewriteBase /subdir/

3. Optionally, if you want to omit the script name in production environment url’s, be sure to have enabled no_script_name = on in apps/<yourapp>/config/settings.yml


prod:
  .settings:
    no_script_name: on

4. If you took step 3, be sure to check if your apache installation has support for mod_rewrite enabled.

This way you can have nice url’s, like http://www.example/subdir/module/action
That’s it!

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-&gt;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.

__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