Archive for category php

How to force symfony colors on windows with PuttyCyg?

Those of you who’re developing with symfony under windows will have noticed that, when running tasks in the command prompt, no colors are used. This is because the windows command prompt isn’t compatible with the color notation.
Most of you also have cygwin installed (shame on you if you didn’t :p). But even if you run the tasks through “PuttyCyg”, which is fully compatible with the color notation, you will not benefit from the colors.

Why?

Read the rest of this entry »

Tags: , , ,

the future of web development

It is always hard to tell what the future will bring, especially in a world where everything is evolving so quickly. But it’s always good to know what libraries, languages or applications will influence the way we work or the language we code in. There for I took the time to create an overview of what could be interesting in the near and far future.  If you have any remarks or other ideas, I would be happy to hear them in the comments.

HTML 5

Jon Von Tetzchner (director of Opera) recently announced that HTML 5 will replace Flash almost completely. Will it go that far? I don’t think so, but what we will be able to do with it is quite impressive!

  • Extension of the DOM with new elements like: section, audio, video, canvas, etc…
  • New attributes like: ping, charset, async
  • Id, tabindex and repeat for every element

As you can see the HTML 5 will be an expansion of HTML 4 but with rather important differences. We can expect HTML 5 to get its W3C Candidate Recommendation in 2012 but it will be able to use some of the features sooner. One of those is canvas that is already a widely implemented element.

Serge Jespers wrote a post on his weblog as a reaction on what Jon Von Tetzchner said and you can read about it here http://www.webkitchen.be/2009/05/27/adobe-versus-the-open-web/ He is an Adobe platform evangelist and Adobe pays his payckeck every month, but I do think the guy has a point.

JavaScript 2.0

With JavaScript being more popular than ever this is certainly worth taking a look at. John Resig created a slideshow on what will be changed in the 2.0 version and I must say that it looks promising.

PHP 6

A lot of people swear by the use of PHP over other language (like me :-) ) and a lot of other people say that PHP just isn’t sufficient enough. They are probably right because there are some insufficiencies in PHP, but in version 6 a few of those will be reviewed and adjusted. For now v6 is available as a developer snapshot so if you want you can already try it out.

Improved Unicode support.
This was a really necessary to keep up with other languages like JAVA that have a better i18n (internationalization) support, in this way PHP will close up the gap it had and support you with a much broader set of characters.

Namespaces
Namespaces are a way of avoiding name collisions between functions and classes without using prefixes in naming conventions that make the names of your methods and classes unreadable. By using namespaces we can never have the problem that 2 classes with the same name interfere with each other. I’m still wondering if this is the best solution but we’ll have to find out in the future. I’m only struggling with the idea that this only moves the problem to a higher level, as you now have to make sure your namespaces aren’t the same.

XML
XMLReader and XMLWriter will become part of the PHP core, which will make it easier to work with XML in PHP.

Magic_quotes and register_globals will be removed from PHP as they are a huge problem in PHP security.

It is from version 6 required to use tags as the ASP-style tags are no longer supported (<% %>)

Those are the biggest changes, again, just like in JavaScript some of these changes are already implemented in a PHP5.3
So in a nutshell, I do believe that PHP has a superb future in front of it, but that is also because of frameworks like Symfony, CakePhp, Zend, etc.

Tags: , , , ,

Mantis in Eclipse with Mylyn

This is a post especially for those who use Mantis as a bug tracker software and Eclipse as development software. For our PHP and JAVA development we do all the programming in Eclipse and wouldn’t it be easy if Eclipse recognized the bugs we are working? Wouldn’t it be easy if we could just get an automatic link between mantis and the commits we do in subversion (svn).

Well, praise the lord, ’cause there is an open source project out there that does all these things called Mylyn.

Mylyn is a task-focused interface for Eclipse that reduces information overload and makes multi-tasking easy. It does this by making tasks a first class part of Eclipse, and integrating rich and offline editing for repositories such as Bugzilla, Trac, and JIRA. Once your tasks are integrated, Mylyn monitors your work activity to identify relevant information, and uses this task context to focus the user interface on the task-at-hand. This puts the information you need at your fingertips and improves productivity by reducing searching, scrolling, and navigation. By making task context explicit Mylyn also facilitates multitasking, planning, reusing past efforts, and sharing expertise.

tasklist-splash-31
After a quick install and doing the configuration in Eclipse the interface is installed in Eclipse and ready to work with. And it is great! Easy overviews of all the bugs sorted by filter as you personally prefer, great layout to create and manage the bugs, etc.

It communicates quite nice with mantis, it could be a bit faster but let’s not fall over that tiny thing. The cool thing though is when working for a specific bug, all changes will be committed on those files with a reference to that specific bug. This makes it very easy to get see what files where changed for a specific bug.

For example when you commit changed files you will be asked to comment your committed files. Mylyn links your files directly to the current bug you are working on and puts this in the comment area: “fixed issue #178: Change user name to full name” . The number of the issue and the state are first mentioned and the title completes your comment.

In the screenshot you can see how a bug can be inserted or edited in a easy layout and inside the eclipse environment. Click on the image or here to see a larger image
mylyn-31-screenshot

My colleague Toni also installed WebSVN that gives us a super overview on all those changes and files committed in subversion.

If you use Mantis and Eclipse and SVN I highly recommend that you use these two utilities.

Tags: , , , ,

TestFest 2009

Tomorrow on the 9th of may an event called ‘PHP TestFest 2009′ will take place in the offices of Combell. (visit the website)

What is TestFest you ask?

The TestFest is an event that aims at improving the code coverage of the test suite for the PHP language itself. As part of this event, local User Groups (UG) are invited to join the TestFest. These UGs can meet physically or come together virtually. The point however is that people network to learn together. Aside from being an opportunity for all of you to make friends with like minded people in your (virtual) community, it also will hopefully reduce the work load for the PHP.net mentors. All it takes is someone to organize a UG to spearhead the event and to get others involved in writing phpt tests. The submissions will then be reviewed by members of php.net before getting included in the official test suite.

This event will take place in different countries but for the dutch speaking PHP community you can go to these addresses:

Hogeschool Domstad, Koningsbergerstraat 9, 3531 AJ Utrecht,The Netherlandstestfest_big-276x300

And

Combell offices Ghent, Skaldenstraat 121, 9042 Gent, Belgium (more info)

There will be a workshop to get the needed information and then it’s your turn to contribute to the development of the PHP language.

Felix De Vliegher is the mentor for Belgium and did the most tests in 2008 in the whole freaking world. With his experience and vision, this will be a piece of cake, be there or be square…. (maybe I will write about Felix later in our topic interesting people)

For those who will be on this event, have fun!!!!

Tags: , , , , , ,

Basic forms with Symfony

SymfonyOur goal is to create a little crud that contains 4 fields

id (auto increment integer)
name (varchar 50)
value (double)
type (0 or 1)

What I will explain is how to change your fields in the form after creation with generation of a module. So we start with a clean module that contains 1 action class and 3 templates (new, edit and index) and 1 partial (form) that is included on the new and edit template.
We created this module not by hand but with the very easy command: symfony propel:generate-module.

Read the rest of this entry »

Tags: , ,

Symfony Form: unsetAllFieldsExcept()

When creating forms in symfony which extends from a Base (ORM) class, you sometimes do not need all fields that Propel/Doctrine generates for you. If you want to remove them you have several ways to do so.

1. Unset the fiels you don’t need.

public function configure() {
  unset($this['field1'], $this['field2'], ... );
}

Simple way to remove the fields you don’t want, but if you change something in your database, which has an impact on that form (new column, foreignkey, etc ), it will be added in the (generated) super class. And if you don’t update the unset function with that extra field, it will be expected in the form. Unless you have unit tests for all your forms this could brake it without knowing it.

2. Override the setup function.

/**
 * @override
 */
public function setup()
  {
    parent::setup();

    $this->setWidgets(...);

    $this->setValidators(...);
  }

Another way is to override the setup function and do your own widgets and validators initialization. This is a good way to only intialize the fields you really need, but you’ll have to do what your ORM generated for you.

3. Use a function which unsets all fields except the one you need (like the enableAllPluginsExcept function).

$this->unsetAllFieldsExcept(array(
      'field1',
      'field2',
      'field3',
      ...));

This would be usefull, because it will remove all fields you do not want to use, and even if new fields are added it will not use them. The drawback is that if you have 20 fields and only need to unset one of them, you’ll have more type work ;) .
Since it does not exist in symfony, I quickly created our own function. This function should be added in the class BaseFormPropel.class:

abstract class BaseFormPropel extends sfFormPropel
{
  public function setup() {
  }

  protected function unsetAllFieldsExcept($fields = array()) {

    $unsetFields = array_diff(array_keys($this->getWidgetSchema()->getFields()), $fields);

    foreach($unsetFields as $value) {
      unset($this[$value]);
    }
  }
}

Which one to use ?
None of them are better. But if you need to reset a majority of the fields the ORM has generated then I would go for the method 2. If you only need to unset the fields you do not want to use then I would use the function unsetAllFieldsExcept().

Which method do/would you use ?

Tags: , ,

sfWidgetFormInputCheckbox unchecked bug

Ever tried to create a Symfony form with a check box input field, which by default should be unchecked ? No ? Well read on because it’s not that easy :) .

This would be the code to print a check box input field in a Symfony form:

class CheckboxForm extends BaseCheckboxForm {
 public function configure() {
 $this->widgetSchema['field'] = new sfWidgetFormInputCheckbox();
 }
}

When you use the form to create something then by default the check box should be unchecked. And when you use this form for editing, then the check box should be checked if the object you try to edit has already been checked in the past.

If you try this code, you’ll see that the check box will always be checked. Even if you display this form to create a new object. Strange no ?

Read the rest of this entry »

Tags: , , ,

Comments to the max

I was wondering around on the big wide scary web and saw a lot of badly written comment for code so I thought it shouldn’t be bad to give a little overview on how comment should be written, at least how I think it should be written.

Not all comments can be written in the same way and not all comments can be read by other programs but I personally think the Java comment is the most readable one, and (huge advantage) can be read by the Javadoc processor. When writing in PHP this is done in the same way as PHPDoc is an adaptation of Javadoc.

So write your comment like this:


/**
 * This is a basic description of your method,
 * write down here what the function does in general
 *
 *@param integer $id
 *@return array Array of objects
 *@author Kenneth van Rumste
 */

Read the rest of this entry »

Tags: , , ,

A few helpful PHP date methods

For a lot of people dates in PHP are an issue, and the PHP functions aren’t sufficient enough to do all converts you need.
Therefore you find a few easy PHP date conversion methods. Feel free to add your own methods or requests in the comments as we know that these methods don’t cover everything. You can see it for yourself when you read the rest of this entry:

  • get start and end date for the next two months in an array
  • get the datetime for a specific user culture in Symfony
  • get all days between a given start and end date and return it in an array
  • get the difference between two dates

Read the rest of this entry »

Tags: , , , ,

PHP DOMDocument: Convert Array to Xml

phpRecently I start working on an export module to invoice applications. One of them uses a very simple xml structure (simple nodes without attributes etc.) and therefore I wanted to create this xml also in a very simple way: from an array.

I decided the use DOM, and not e.g. SimpleXML, because I need to validate my xml with a schema.

After a quick search on the internet I found several snippets that could do the trick until I had to set the same element tag name on the same level:

<nodes>
	<node>text</node>
	<node>text</node>
</nodes>

The snippets I found use the index of an array to create the name of the element and the value for the text part. Only everyone knows that you cannot set 2 keys with the same name in an array.

So what could we do to improve this ?

Read the rest of this entry »

Tags: , ,