Monday, November 10. 2008
State of annotations in the PHP world
Annotations are a really helpful feature in present-day development. An annotation is a special form of syntactic metadata that can be added to source code elements such as classes, methods, properties and parameters. They do not affect the program semantic directly, but can be used by tools and libraries to handle such annotated code in a certain way. Examples for such usage are marking classes or methods as accessible via web services, marking methods as unit test methods, or to define how an object should be persisted.
Annotations are mainly known from the Java programming language, and since Java 5.0 annotations are a language feature. More important, these annotations are accessible at runtime, which allow various new meta-programming approaches where the domain object stays very clean and does not have any references to the handling code, i.e. when persisting domain objects or rendering them into XML. Today I want to take a look on the state of annotations in the PHP world. PHP itself does not offer such a feature, so we have to take a look at userland implementations. Such implementations are possible using the reflection API introduced with PHP 5. When looking at userland implementations one has to differentiate between specialised and generic implementations. Continue reading "State of annotations in the PHP world" ![]() Wednesday, August 6. 2008
Speed, speed, speed!
As requested I did my homework today and tested Stubbles against PHP 5.3 alpha1. Well, actually alpha2-dev, but that should not make that much difference. I don't know whether 5.3 is PHP on drugs, but runtime of our unit tests dropped from around 45 seconds with 5.2.5 to around 15 seconds on 5.3. Down to one third! This is really impressive. We will have to see how 5.3 will perform with the applications itself, but this raises expectations.
The other good news is that we only had two forward compatibility issues, which I instantly fixed. One of them was in a unit test and not relevant to the framework itself, the other had to do with a new check on callbacks which now forbids using private methods in callbacks, even if the callback is triggered from inside the same class. Update At first I thought of just another check for the callback thing, but the comments lead me to reinspect the code. I now think it might really be a bug in PHP 5.3, therefore I created a small script to reproduce the effect, see repository. I sent it it to Lars, who will make sure it gets fixed in the final release. ![]() Tuesday, May 27. 2008
JSON-RPC Proxy Generation with PHP
Today I gave a talk about JSON-RPC Proxy Generation with PHP at the Dynamic Languages World Europe 2008 in Karlsruhe Germany. The presentation covers an introduction to the Proxy and Remote Proxy Design Patterns, JSON in JavaScript and PHP as well as the JSON-RPC protocol specification. Furthermore, the slides also cover the SMD specifications and show how to generate SMD with PHP.
Although Stubbles provides a ready-to-use processor for JSON-RPC and generating JavaScript proxy classes, the presentation takes you through all necessary steps that are needed to implement this on your own. You can either view the presentation on slideshare.net, or get the PDF in our download area. If you decide to use our implementation, you can find more information in the Stubbles manual. ![]() Tuesday, March 11. 2008
Searching for a new job?
Ìn case you are interested in a new job and speak german: Read on after the break.
Continue reading "Searching for a new job?"
![]() Monday, February 18. 2008
Traits for PHP
Just stumbled over a proposal for traits in PHP, proposed by Stefan Marr. Being a bit sceptical at first I came to the conclusion that this would be a really useful feature - and save us a bunch of doubled code in Stubbles. After reading the RFC until the end I noticed that I saw a similar concept some weeks before in Java: Qi4J tries to achieve the same, but requires very much code to get this done. As a language construct within PHP it would be very easy to do the same, but easier to learn and to use. Beside some keyword issues (the RFC uses "use" as keyword, but this will of course conflict with namespaces) I'm wondering what this will have an impact on reflection - the RFC stays unclear about this. What do I get when reflecting a method from a trait, or a method in a class that originally comes out of a trait?
However I'm not very confident that this feature will make its way into PHP - maybe I've read to much internals... ![]() Saturday, January 26. 2008
Do not trust the reflection API
At least if it comes to internal classes. If you try to get informations about parameters from methods of internal classes - forget that. Examining several internal classes my key findings are: either there is no information about parameters available and the reflection API says the method does not have any parameters, or the information about the parameter is wrong. Some examples I retrieved with the following script:
Continue reading "Do not trust the reflection API" ![]() Wednesday, January 2. 2008
vfsStream 0.3.0 and good bye SimpleTest
Against my intent of starting to work on file modes for 0.3.0 I decided that it is time to get an own repository for vfsStream in order to maintain it independently from Stubbles, which was the perfect reason to open a new project on Google Code. I will see if this proves to be a good solution for day to day development.
The movement also marked the reason to try PHPUnit because I wanted to make use of its code coverage and its software metrics features. Especially the last one showed me a bunch of points that required refactoring before starting something big like supporting file modes. During refactoring some improvements made it into the code which justified a new release: vfsStream now supports rmdir(), and I made some API changes. I dropped vfsStreamDirectory::ceate() in favor of vfsStream::newDirectory() to be equivalent to vfsStream::newFile(). Additionally there is a new vfsStreamContent::at() method now, which allows to write code like this:
$parentDir = vfsStream::newDirectory('newdir')->at(vfsStreamWrapper::getRoot());
$file = vfsStream::newFile('file.txt')->withContent('foo')
->lastModified(time() - 86400)
->at($parentDir);
So far I'm very satisfied with the software analysis features of PHPUnit that I'm thinking very hard about saying good bye to SimpleTest in my other projects. Given the current state of SimpleTest's development I do not expect to get similar features in SimpleTest any time soon, and unfortunately I do not have the time to contribute to SimpleTest even if it would be a lot of fun I guess. ![]() Saturday, December 29. 2007
vfsStream 0.2.0 released
Having done more work on vfsStream I felt it is time to do the next release before starting to work on the next big issue™. The new 0.2.0 release focuses on API improvements as well as support for differentiating between directories and files. API improvements are a new vfsStream::url() method to assist in creating correct vfsStream urls. Up until now you had to write something like
$url = vfsStreamWrapper::PROTOCOL . '://path/to/file.txt';which is kind of ugly and unhandy to type. Now you can do $url = vfsStream::url('path/to/file.txt'); which should be easier and less to type.Another API improvement is the new vfsStream::newFile() method to be able to do $file = vfsStream::newFile("foo.txt")->withContent("bar");Following an advice by David Zülke I changed vfsStreamWrapper::register() to reset the directory root to null. Additionally vfsStream now supports is_dir(), is_file() and is_readable(). However there is still a small drawback to is_readable(), as long as file mode is not supported existing pathes will lead to true, and non-existing pathes to false. Currently all files and directories always have 0777 as file mode. As I require support for file modes now this will be part of the 0.3.0 release. Another thing I found during the last days is that realpath() seems only to work with real files, but not with any other URL types. That's a bit sad because it means one can not use this function if one wants to test its file operations with vfsStream. ![]() Friday, December 14. 2007
vfsStream 0.1.0 released
Some minutes ago I released the first version of vfsStream. vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with any unit test framework, like PHPUnit or SimpleTest.
The first idea for this came up when I prepared my talk about stream wrappers for the PHP conference in Frankfurt-Mörfelden earlier this year (slides still available). Everybody knows that you should not use real databases in unit tests, so why should one use the real file system? Of course you can create an IO abstraction layer, but PHP offers such easy access to file contents via its file_get_content() and file_put_content() functions as well as with its other file system functions that it would be a bit overdone to create such a layer just for testing purposes. Additionally there are stream wrappers which allow you to create your own protocol and use it with exactly this file functions. This was the point where I started thinking about it, so hacked together a first version as a prove-of-concept. Because it worked like a charm I started to use it in my own unit tests, which already saved me some time and problems. Today I decided it is time to release this as open source to the public to see if other people like the idea and have a use for it, therefore I did some refactorings, and did a first release. Unfortunately not everything works by now: It is currently not possible to rename a file or directory, and it is not possible to remove directories. Additionally file modes are completely ignored. The main reason for this is that I did not need one of this features until now. Feel free to send patches, wait until I have a need for it or beg me to implement them. ![]() Wednesday, December 12. 2007
Tricky namespaces? No.
So I put down this namespace examples together last night. But, what's happening in the examples regarding to resolution rules? (I won't post the code here again, have a look at the last entry for the example code.)
Version 1 and 2 two are the same, the place where we include other code does not influence naming rules in the current file. Why do we get instances of PHP's internal DateTime class instead of the one from Name::Space? Well, two things have to be considered: 1. We are in the global space, PHP will only look for DateTime in the global space. 2. The use-Statement declares that we want to create a shortcut to Name::Space, not to Name::Space::DateTime. If we wanted to use the class from Name::Space we would need to write $d = new Space::DateTime, which is version 3. (We can not do a use Name::Space::DateTime here, this results in a fatal error. However, we could do use Name::Space::DateTime as DT and then write $d = new DT(), this will give instances of Name::Space::DateTime.) With version 4 we have put our example code into its own namespace, but the result is still the same as in version 1 and 2. What's happening? 1. We are in namespace foo, and in namespace foo no class DateTime exists. 2. Our shortcut use Name::Space still does not refer to Name::Space::DateTime, to create instances from this class we still need to write $d = new Space::DateTime as in version 5. This means the class does not apply either in version 4. 3. As no further use declarations exist, PHP falls back to its internal DateTime class. (If this class would not exist, a call to the registered autoload function would be issued.) Finally, version 6 gives the result we originally intended: The class Name::Space::DateTime is used instead of PHP's internal class. This is because we stated in the use declaration that we want to use Name::Space::DateTime as DateTime. However, in the original posting of David Zülke in php internals he feared that the two instances of $d might be from different classes. Well, this might really happen under some circumstances. More after the break... Continue reading "Tricky namespaces? No." ![]() Wednesday, December 12. 2007
Tricky namespaces
Today on php-internals David Zülke posted an interesting example with namespaces which made me think about the result of the small example. Well, my first guess was not right as my experiments showed. Therefore I tried different versions of the example to see the impact of namespace and use declarations. But first, the example:
Name/Space/DateTime.php
namespace Name::Space;
class DateTime {}
Name/Space/Ship.php
namespace Name::Space;
class Ship {
public static function __autoload($className) {
echo "loading $className\n";
// ... autoloader, can load Name::Space::DateTime
}
}
somestuff.php
function someStuffWeDoNotHaveControlOver() {
$foo = new Name::Space::DateTime();
var_dump($foo);
// ...
}
setup.php
require_once('Name/Space/Ship.php');
spl_autoload_register(array('Name::Space::Ship', '__autoload'));
Version 1, the original from David's posting:
require_once('setup.php');
use Name::Space;
$d = new DateTime();
var_dump($d);
require_once('somestuff.php'); // assumed to be here, was missing in the given example
someStuffWeDoNotHaveControlOver();
$d = new DateTime();
var_dump($d);
Result:
More after the break... Continue reading "Tricky namespaces" ![]() Sunday, November 11. 2007
Gathering practical experience with namespaces
Being a little bit bored today I started to port XJConf to PHP 5.3 using namespaces. Oh boy. You should never even think about porting your application completely to namespaces if you do not have enough unit tests! Consider something like this:
if (get_class($instance) == $someClassName) {
// do this
}
It is most likely that $someClassName does contain the name of the class only without the namespace prefix. As get_class() now returns the full qualified class name this comparison will fail - you need to make sure that $someClassName contains the full qualified class name. I'm sure there is a lot of code out there like this or similar. Another point is that you should not import classes from the same namespace, I had a situation where this lead to a fatal error because of loading the same class twice, however I was not able to create a reproducable test case after I solved the problem. As I said I would not recommend the transition without unit tests, but you have to adjust them as well. For XJConf I just had to change two things (I'm using SimpleTest): 1. expectException() requires the full qualified class name of the expected exception now 2. creating mock objects: Mock::generate('Tag'); needs to be changed to Mock::generate('net::xjconf::Tag', 'MockTag'); to enable SimpleTest finding the correct class (full qualified class name), additionally you need to state a class name for the mock class to be created, else SimpleTest will fail to create the mock class because it would produce something like Mocknet::xjconf::Tag which of course results in a parse error. Finally __autoload() makes really fun now. It receives the full qualified class name as argument, it is a simple task to change this into the correct path to the class file. If you do not use it until now due to the quirks required when not using class names like My_Application_Has_Really_Really_Long_Class_Names you should really consider using it after migrating your application to namespaces. ![]() Saturday, November 10. 2007
Subtle BC break in PHP 5.2.4
Today while evaluating PHP 5.3 I stumbled about a subtle BC break which has already been introduced in PHP 5.2.4. Consider the following code:
class Bar
{
public function dumpBar()
{
var_dump(get_object_vars($this));
}
}
class Foo extends Bar
{
public $public = 'public';
protected $protected = 'protected';
private $private = 'private';
public function dump()
{
var_dump(get_object_vars($this));
}
}
$foo = new Foo();
$foo->dump();
$foo->dumpBar();
Continue reading "Subtle BC break in PHP 5.2.4"
![]() Thursday, November 8. 2007
Slides from IPC 2007
I just put the slides from the presentations at the International PHP Conference online, you can find them in our downloads area. Unfortunately Stephan became ill some days before the conference, so I had to present the talks we intended to do together without him. However I think they went quite well and I hope everybody who listened got some new ideas for his programming work.
Those who attended my session about Dependency Injection might be interested in our new documentation for the Stubbles IoC container. It goes more into detail on how it can be used than it was possible to do in the session. As a side note, Robert Lemke had a very good session on Dependency Injection and AOP and how this is possible in the upcoming version 5.0 of the Typo3 framework. From those sessions I attended I think this was the best one. ![]() Tuesday, November 6. 2007
Tutorial for XJConf for PHP at IBM DeveloperWorks published
Just today a tutorial on XJConf for PHP was published on IBM DeveloperWorks: Process XML configuration files with PHP by Vikram Vaswani. This tutorial gives a very nice introduction into XJConf and what it is good for. I'm very thankful to Vikram Vaswani because the tutorial covers very much of the XJConf basics we should already have created user documentation for but did not due to lack of time. Additionally the article comes just in time for my talk at the International PHP Conference tomorrow morning where I will speak about Dependency Injection and how XJConf can help you with this (in parts).
![]() ![]() |
![]() ![]() Calendar
![]() ![]() Categories![]() Quicksearch![]() Blogs we read...![]() Syndicate This Blog![]() Blog Administration![]() ![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||




