Claudiu Persoiu

Blog-ul lui Claudiu Persoiu


Archive for 22 May 2010

PHP type hinting for scalar data types, a step closer

without comments

In PHP 5 a new concept was introduced, type hinting. This feature allows validation of a parameter for a certain type.

This validation type is very popular in object orientated languages and I believe it represents a plus for the PHP object model and even more for his dynamic nature it self.

In PHP 5.1 validation for the array data type was added.

Let’s take a small example:

class a { }

class b { }

function testa (a $a) {
    echo "Bla bla\n";
}

testa(new a());

testa(new b());

The result is:

Bla bla

Fatal error: Argument 1 passed to testa() must be an instance of a, called in ...

Cool, ha?

The type in which we can validate can be a class, an abstract class or even an interface which is inherited  by other classes, like:

interface inherited { }

class a implements inherited { }

class b implements inherited { }

function testa (inherited $a) {
    echo "Bla bla\n";
}

testa(new a());

testa(new b());

And the above example will not raise any errors.

Unfortunately there is no way to validate primary (or scalar) data types. When PHP 5.3 was being released there have been some discussions on the subject, but it seems that it was too late, and the patch did not make it to the final version.

Yesterday, while I was doing my morning reading at a cup of coffee, what do I see on Ilia Alshanetsky blog: Scalar Type Hints are Here! Well, is a little exaggerated, there are not exactly here but there really close. Basically there are in the SVN trunk and will be available in the next stable version!

Of course that even in the current version of type hinting is not exactly mandatory to use it in PHP, is more of an issue of fine tune.

Basically we could already validate to a certain primary data type, but still:

function testint($var) {
     if(!is_int($var)) {
         trigger_error("Type must be Integer", E_USER_ERROR);
     }
     .......
}

is not exactly as elegant as:

function testint(int $var) {
     ......
}

Written by Claudiu Persoiu

22 May 2010 at 7:02 PM

Posted in PHP

CodeIgniter 1.7 Professional Development Packt Publishing – Book review part 1

without comments

0905.jpg

Packt Publishing has published a new book on the RAD PHP framework CodeIgniter version 1.7. The book CodeIgniter 1.7 Professional Development written by Adam Griffiths, has the slogan “become CodeIgniter experts with professional tools, techniques and extended libraries”.

Just from the slogan you can see a slightly different tone then the book CodeIgniter 1.7 from Packt Publishing, which in my opinion has the purpose of showing what the CodeIgniter framework can do.

This book has the purpose of showing how can you develop professional applications, or at least until proven otherwise, stay tuned to find out!

An argument for the slogan is the target audience like is defined in the “Who this book is written for” section, which contains the phrase:

Basic knowledge of CodeIgniter will be helpful.

A dangerous phrase in my opinion, because it can scare off a beginner, even though in the book it looks like all the steps are described starting from server installation.

If from the first book I was expecting a general presentation of the framework, now I’m curios to see if the examples are more ample and more concrete.

From the sample chapter it looks like there is a lot of code. As long as it is logical I thing it’s a good thing, a lot of times is easier to understand code, which you will after all write and you can take as an example, then theory.

But more about this book after I’ll get to read it.

To be continued…

Written by Claudiu Persoiu

11 May 2010 at 10:31 PM

Posted in PHP

Tagged with ,

PHP as a general-purpose language

without comments

PHP for desktop, is it worth it?

PHP is described on Wikipedia as:

PHP: Hypertext Preprocessor is a widely used, general-purpose scripting language that was originally designed for web development to produce dynamic web pages.

Taking this into consideration, apps divide in 3 categories: web, command line and desktop.

For the Web, PHP is the most popular open-source (and not only) programming language.

PHP CLI

PHP CLI (Command Line Interface) I find it very interesting, even though is not used at it’s full potential. A lot of developers prefer shell scripting or Perl with no real reason. I’ve been playing around with this tool and I liked the result.

Important frameworks like: Zend, Symfony or Cake PHP are using PHP CLI to generate projects, models, CRUD or other features that can be easily used with a command line.

In the Windows environment the command line is not exactly popular, but in Linux is almost imperative. After all what’s the point of using shell scripting when you can use a powerful language with a lot of features like PHP?

But CLI is not limited just to the command line, it is usually used for cronjobs, pipes, socket-servers etc.

PHP-GTK

When it comes to PHP and desktop usually people thing about PHP-GTK. What do I thing about the project? Is not dead, like it says on the official website, but is not exactly alive. The reason? Gtk is not exactly simple. If you come from the Linux environment probably is not that difficult, but if you usually work on the web is not exactly html… Nevertheless there is a community that keep this project alive.

Nevertheless, it allows developers to build desktop apps in PHP, compatible with a wide variety of operating systems.

But there is an issue, the resulting apps are not exactly compiled code, they must run using a PHP virtual machine. Here is the issue, how do you distribute the app? If you have a small app of only few lines of code, to distribute it with a virtual machine is a little complicated… Also the code is visible, of course there are methods to solve this issue, but there aren’t exactly simple.

This is probabily the most popular PHP platform for desktop, if you can say that about this environment.

The documentation is pretty big, it was taken from the C++ version. Is not as well polished like the PHP manual for instance, but I believe is sufficient.

Winbinder

Compared to PHP-GTK it has an disadvantage, it only works on MS Widows operating system. The advantage is that it has a much simpler API. If I had to choose a PHP platform for desktop, probably I would go for Winbinder. Unfortunately is in the same state, is not dead but is not exactly alive. It also have the support of a community, but without any particular special results.

The issue with the compiled code is found here also, and even more the issue with platform distribution is just as bad. I firmly believe that if you want to develop an app using this platform, to make it work on your computer is the easiest part, to make it work on someone else’s computer is the real issue…

The documentation is pretty small, because of the API. But simplicity is good when it comes to programming, that means you can easily build pretty interesting apps.

Compilers

There a few, and the majority of them have some issue because they use old PHP versions of even old GTK. I’ve spend many hours on Google trying to find some reals solution but with no success.

Most popular compilers:

  • Bambalam – works well with CLI and Winbinder. But it has a great disadvantage: it’s only compatible with PHP 4.4.4, and I believe that says it all. Anyway I believe it was the most interesting solution, unfortunately to old (the last version was released in 2006).
  • PriadoBlender – works well with PHP-GTK and CLI, but is not very stable. The last (beta) version was released in 2007, and since then nothing new was ever heard from it. Probably if that version would be updated  it would help a lot the PHP-GTK project.

Conclusion

When it comes to Web, everything is great!

PHP as a command line tool is ever more popular and more tools appear each day!

In the desktop environment is a “the living dead” sensation… This projects are not dead but there not exactly alive. Of course there are other solution for PHP on desktop which I did not mention, but there are in about the same state. Probably a new approach would help, something more attractive for web developers and those passionate about this language.

Written by Claudiu Persoiu

8 May 2010 at 5:00 PM

Posted in PHP

Tagged with , , ,