I wrote my first application on PHP

17 09 2012

Why?

A friend needs a small application to sell some courses. The business model will make the use of a proper shopping cart (like Magento) overkill in some aspects and, in the other hand, some specific price rules and the payments process (Integration with Latin American Payments) will need a extensive development.

The hosting was on GoDaddy, so the only option was PHP.

So, here are my opnions

The language

Some readers from years ago (and my close friends) will know that I don’t like PHP. And when you read some horror stories like PHP: A fractal of bad design

My not-at-all-extensive experience was:

  • The max PHP version that GoDaddy support is 5.3.10. So maybe some issues are resolved on later releases, this also limit my framework choices (more on that later)
  • Speaking about frameworks, I suspect that my Framewrok of choice (Yii) hides some language’s oddities from the developer
  • I like the instant-no-redeploy nature, That feels very productive coming from a Java experience (Some frameworks like Play and Grails have this feature too, also JRebel could make this on plain Java projects, but I don’t test it yet)
  • In the other hand that sense of productivity falls when you need to type those strange characters that the language designers love. Also this makes the code very hard to read, Some examples
    • “$” as prefix of every variable and parameter
    • “->” to call a “instance” method. (I don’t know if the language designers have this concept clear)
    • “::” to call class or static method (More on that later)
    • “$this->” as prefix of every instance method call inside the same class (This makes me thing that the whole OOP features are no more than makeup)
    • “self::” as prefix of every class or static method call inside the same class (Please note, that “self::” is a reserved word that represent a “class” but “$this” is a variable that represent the actual instance, “this” isn’t a reserved word. So you could have
      const this = 3

      . Also note that the reserved word is “self::” not “self”, So you could have

      const self = 3

      and continue to use

      self::staticMethod()

      but you could call your instance methods like this

      self::instanceMethod()

      So really “self::” could be the “class” or the “instance”. As you could see, the “fun” with PHP never ends ;)

      UPDATE Sep 18 2012
      Seems that “self::” or “self” aren’t reserved words at all… Oh, the humanity
      END UPDATE
      )

  • The “array” thing that isn’t array nor a dictionary, but could be anything and both at the same time (PHP arrays could be use as metaphor in the Arminian-Calvinist controversy).

    The whole syntax is ugly and very hard to read, and worst of all, could be very error prone, Example (actual code):

    return array(
                array('allow', // allow all users to perform 'index' and 'view' actions
                    'actions' => array('index', 'view'),
                    'users' => array('admin'),
                ),
                array('allow', // allow authenticated user to perform 'create' and 'update' actions
                    'actions' => array('create', 'update', 'course', 'user'),
                    'users' => array('@'),
                ),
                array('allow', // allow admin user to perform 'admin' and 'delete' actions
                    'actions' => array('admin', 'delete', 'pending', 'ready'),
                    'users' => array('admin'),
                ),
                array('allow',
                    'actions' => array('response', 'confirmation'),
                    'users' => array('*'),
                ),
                array('deny', // deny all users
                    'users' => array('*'),
                ),
            );
    

    And I choose not the worst example, this could be grow quickly in a huge mess.

    IMHO, PHP needs named parameters very badly, and the developers need to take a more OOP approach to clean this mess

  • Errors are terrible, Yii will show you some nice error report (But some times isn’t clear where the error is), but other errors will show you a blank page, no design, no purpose, no evil, no good, nothing but blind, pitiless indifference

The framework

I examine some of the myriads of PHP frameworks

  • CodeIgniter was my first option, I like what I see, but in some cases is bare-bones and have a lack of functionality that already come with other frameworks
  • CakePHP 1.3 come with many nice things but is also very restrictive
  • CakePHP 2.0, FuelPHP and Symfony 2.0 don’t have support for the PHP version that I have (I’ll like to test Symfony 2.0, its seems very Java-like)
  • Yii was my option, the last stable version (1.12) have support for my PHP version, is very well documented and are very feature rich
    • Some of my toughs:

      • As I told you before, I suspect that Yii hides some language oddities
      • The framework is well structured, and no more different from other frameworks like Django or Grails
      • Gii, a nice GUI for generate code (Models, controllers, forms, views) is a huge save time, but isn’t so powerful. The code generator that come with Grails is more powerful, and the Django Admin is almost perfect (in fact I’ll love make this project with Django)
      • Yii comes with a great widgets selection, but the syntax to use isn’t so nice (“array” based)
        widget('zii.widgets.jui.CJuiDatePicker', array(
            'attribute' => 'birthday',
            'model' => $model,
            'options' => array(
                'dateFormat' => 'yy-mm-dd'
            )
        ))?>
        //There's another alternative syntax, but I don't test it
        
      • Yii have hundreds (if not thousand) of extensions, but almost every extensions that I try fails miserably.

      The IDE

      I use IntelliJ as always, with the PHP plugin, it’s a very nice environment and have integration with an FTP client, so every change that you made on the code is auto-magically synchronized with the server, that could ensure some errors in production and hilarity.

      The Yii community likes PHPStorm that, in theory, is a Slim-down IntelliJ with only PHP/HTML/CSS/JS/SQL functionality.

      Conclusion

      My experience with PHP, was mixed, The language is really bad, but the instant-redeploy-on-server is nice, also Yii made a great job giving some sanity and features for free.

      I really hope that this will be my last experience with PHP, other options like Django or Grails are much better.

      About these ads

Actions

Information

10 responses

18 09 2012
banetbi

Most all of your complaints about the syntax, except use of the $ prefix, exist in C++ as well. Personally I think the $ makes it much easier to find the vars when scanning through a piece of code and eliminates confusion over whether something is reserved word or a variable.

18 09 2012
Mario Arias

The fact that some language constructs are used in C++ too, don’t make them less ugly.

More modern languages (An by “more modern” I mean 20 years ago) use the dot (“.”) notation that is much clear, but PHP use “.” to concatenate strings instead of the more natural “+”.

IMHO The “$” notation don’t have a real advantage, if you use a programing language everyday you MUST know the reserved words, and the editors or IDEs will help you.

Sound code conventions will help the programmers to read code. The sad problem is that PHP itself in the core don’t have code conventions, some built-in functions have camelCase notation others have underscore notation, some “built-in” functions are really reserved words and so on. In that context the “$” notation have sense

18 09 2012
Pierre

Symfony2 supports PHP 5.3.3, so it would’ve worked on the 5.3.10 you had to work with.

I just started with Symfony2 a couple of months ago, and I wouldn’t recommend it for PHP beginners, but I would definitely recommend it to anyone needing to build a large scalable application.

The syntax of PHP is also IMO only personal preference. Yes there are some (a lot) of inconsistencies with regards to functions and parameters, but it’s something you get used to when you use the language a lot. Some developers (not including myself) don’t like the syntax of E.G ruby or python but prefer PHP and finds it easier to learn and get something done quickly.

18 09 2012
cayhorstmann

I am doing some Java EE grunt work with GlassFish and its Eclipse plugin right now. I get instant redeploy on save. No different than PHP or Rails. Fun and productive. But I am afraid I can’t deploy on Godaddy :-(

19 09 2012
hafizan

-> vs . It much easier consider as subset . You don’t need ugly nested array.. $x[‘test’][‘m’]=’x’ .Is multi dimension array.. much cleaner.. + sometimes kinda da bad because it’s dynamic type. So “1”+”2″ .Should output what 3 or 12? .If strict type it wouldn’t be much problem… but converting each type string to int ,int to string kinda slower the process of code itself.

19 09 2012
Mario Arias

You’re confusing dynamic type, with no-type-at-all. Even when PHP variables don’t have type, the values have a type that is evaluated at runtime.

Could be problematic if you try

1 + "2"

But the languages have rules to operate this, or don’t operate at all. This isn’t dymanic vs static typing, but weak vs strong typing.

But

"1" + "2"

Is just an operation between two values that have the same type,

19 09 2012
itoctopus

I think that you have a very wrong first impression about PHP. At first, I used to find the $ when declaring variables a bit weird – but now I find it very powerful, simply because the use of $$, $$$ and so forth.

You can’t judge a language from just one project.

19 09 2012
Mario Arias

So, you’re tell me, that PHP is very powerful ’cause you can have variables named like, $var, $$var, $$$var and so on?

19 09 2012
banetbi

Actually in PHP $$var would create a variable variable http://php.net/manual/en/language.variables.variable.php which is a little confusing unless you have needed that functionality for something. I have only used variable variables a couple of times, but when I needed them they were pretty sweet.

19 09 2012
Evan Edwards (@JWokky)

As you pointed out, you were hampered by an older version of PHP. Much easier than array() is:

return [ “value”, “key” => “value”, “key2″ => [ “value”, “value”, “othervalue” ] ];

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.

%d bloggers like this: