The beauty of PHP is that people can modify it to work however they want due to it’s open sourced nature…
If something annoys you about the programming language itself, you can just go ahead and change it. The upside to that is that you get the features you think are missing in PHP. The downside is that your code can potentially become incompatible with everyone else, and your code becomes quite unportable.
Nonetheless, Ilia Alshanetsky (author of php|architect’s Guide to PHP Security
) has posted on his blog a patch of PHP 5.3 that provides proper, traditional, type hinting, currently unsupported for scalar types.
As he explains, type hinting is a controversial topic; what with the fluidity of PHP and it’s loosely-typed nature. However I think type hinting, especially strict type hinting would be more advantageous than disadvantageous. He explains how his patch adds a certain amount of flexibilty for numbers within strings (using the “numeric” pseudo-type) allowing a string to be passed off as a number (perfectly legal in PHP… e.g. “1″ + “1″ = 2), or alternatively using the stricter “int” type to specify a number and must be of integer type i.e. “1″ is not valid, but 1 is.
Unfortunately I don’t like delving into compiling stuff, so I’ll just have to hope it gets included in the main trunk of PHP… until then I can pretend I have type hinting, perhaps by adding comments such as this messy mess… *ahem*:
function foo(/* int */ $bar)
I’m an advocate of tidy, well commented code, and something that has bugged me for a little while was the messiness of the ternary operator (?:) in PHP. With the introduction of PHP 5.3.0, we can now miss out the duplication messiness of using the ternary operator for existance checks for example. Instead of:
$value = $a_value ? $a_value : $b_value;
We can now simply do:
$value = $a_value ?: $b_value;
It goes without saying that one should still not nest ternary operators as they become messy and extremely difficult to understand, even with good commenting. Consider an if/elseif/else or switch instead.
There are a few other things in PHP 5.3.0 that I’m looking forward to as well, one of which is the bundling of ext/phar which is really rather cool.
There are other dubious things, which are covered (and argued) in great detail elsewhere such as the goto operator and the introduction of namespaces…
I’ve started giving Netbeans for PHP a try, considering everyone is saying how much better it is than Eclipse PDT. So far I’m very impressed at it’s simplicity and ease of use – which for the main tool a programmer uses day in an day out for at least 8 hours, is a very good thing.
However I came across a couple of “issues” or teething problems rather, that thankfully with a short Google (sorry Bing…), I managed to find solutions for…
Read the rest of this entry »
If you created a project in Eclipse, but you created a regular “Project” instead of a particular project type (in my case, a “PHP Project” then there is a fairly easy way of fixing it. I had to do this to make a particular project work in the PHP Explorer (which for some reason worked fine on Windows version of Eclipse, but not my version…)
Create a new project of the type you want to convert your existing project to. In my case, I just created a new PHP Project. Alternatively, you can use an existing PHP Project (or project of the type you want to convert to) if you have one. Open the .project file of this, and copy the <natures> and <buildSpec> XML sections. Open the .project file of the project you wish to convert. Remove the old <natures> and <buildSpec> sections and replace with the copied section. If you have other <natures>, for example PDE etc., then you’ll have to merge them by hand. Mine now looks a little like this:
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>someprojectname</name>
<comment></comment>
<projects>
<project>otherproject</project>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.dltk.core.scriptbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.php.core.PhpIncrementalProjectBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.php.core.ValidationManagerWrapper</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.php.core.PHPNature</nature>
</natures>
</projectDescription>
It’s as simple as that! Close the project and re-open it and you’ll have all the magical features of PDT (or whichever project type you wanted…)
As with everything I do, I hack together solutions when there’s probably a much better and easier way of doing things. But anyway – if there’s not an easier way, then here’s a solution to my problem, and if there is, well… I’ll just use that in future…
My problem is that I want pretty URLs for my app, such as http://blah.com/home/folder/5/item/27. The URL structure of Zend Framework means that the URL has to look like this: http://blah.com/home/index/folder/5/item/27. There’s probably a way around that (my original idea was to use http://blah.com/home/?folder=5&item=27 but that’s just ugly), but I’ll settle for using the pointless verb “go” as the action name, resulting in http://blah.com/home/go/folder/5/item/27. But I also want that goAction to be called when I just go to http://blah.com/home/. I had a quick look at the ZF documentation, but I couldn’t see much about setting a default action in a controller, so here’s my solution:
class HomeController extends Zend_Controller_Action
{
public function indexAction()
{
// ... action code here ...
}
public function goAction()
{
$this->_forward('index');
}
}
As I said, there’s probably an easier way around this, but this works for me, so ner!
Update: As always, there is an easier way of doing this and I was reading the wrong part of the documentation. As Patrick says in his comment:
Hello, there is a way to specify default module, controller and action for each active route in the Zend_Controller_Router used by Zend_Controller_Front.
http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard.variable-defaults
just pass an array