Archive for PHP

Is Rails suffering the same problems as PHP?

I know that mentioning Rails and PHP in the same sentence is taboo and can incite many flame wars. But hear me out — Some say one of the “downfalls” of PHP is that its so easy to write applications and just about anybody can write a PHP if they put their mind to it and learn the syntax. As a result, PHP Applications have a long history of badly written, insecure code and has really been shunned by many advanced developers (aside from many poorly named methods, I think PHP is alright!).

In recent months I’ve been recruiting for a Rails Developer at my company, in Chicago. I did a phone interview and the guy couldn’t name me any rails sites he reads, or name the 7 rest actions, or even tell me what version(s) he uses. Now, I’m not claiming to be an expert, but I would think that even an average Rails developer would know that.

I’ve also seen code where the developer doesn’t quite get MVC. You don’t need to be validating values in your controller and adding error messages to @model.errors.add for example. You don’t need to be setting up display labels in your model. You don’t need to be counting things in a helper method. I was thinking, why is it so hard? Is it because in mostlanguages we have had free reign to interpret MVC however we pleased? I know I spent many years in PHP trying to develop a DAO. I made an object that returned a row of data, with methods to access the fields. But what about a collection? what is that? I experimented with an array of objects but I felt that was too wasteful. Iexperimented with a DaoSet that was made to hold a collection — complete with iterators! I even struggled with MVC and knowing what went where…when I saw Rails it was like someone who met their true love — this is it!

After experiencing Rails, I realized hey I can do some of this in PHP! I wrote a simple MVC framework in PHP. I called it StupidlyEasyMVC Framework. I was able to use it at work (at the time, I was still at a PHP job. Some say I talk about that time in my life as if it were a prison sentence haha). SO I was able to apply things I learned from Rails, into an existing structure at work. Just like the guy from CDBaby — I couldn’t drop my existing PHP site and just start converting to Rails, but by starting to gradually switch from what I had to an MVC structure, it made my code cleaner and easier to work with.

So as I look for advanced rails developers, I find they are far and few between. Either they are hotshot consultants or working on their own startup. What makes a rails programmer “get it” and really work with the framework instead of against it - still trying to write as they always have?

Here are some things you can do if you feel you are still writing PHP or .NET code in rails..

Get a project. You can read and dabble all you want, but if you don’t have a project, you won’t be forced to learn. Along with that, you need a timeline to force you to keep at it!

Watch all the Peepcodes! especially the REST and rspec episodes. I’ve search hard and long enough for answers, to find comprehensive training in ONE spot, in such an easy to learn format is awesome. I constantly talk about peepcode and I won’t work with anyone who hasn’t watch them.

Peepcode Code Review - their first book, full of helpful hints and gotchas that will really help you move beyond a beginning developer

Ruby Inside Blog - www.rubyinside.com

Jay Fields - http://blog.jayfields.com/

Obie Fernandez - obiefernandez.com

Books:
Agile Web Development with Rails - If you are doing Rails and don’t have this book, get it! It is the bible of rails! I don’t care if you have the first edition already, this will save you time!
The Ruby Way by Hal Fulton - because if you don’t know ruby, you will never be a good rails developer.
The Rails Way by Obie Fernandez - its good because I tech edited it before publication! It is a comprehensive instructional and reference book — all in one. I cannot wait to get my hands on a paper copy of this to keep at home and one for work. (just got one this week…I’m ecstatic)

Well this has been mostly a rant but hopefully a help resource to move into more advanced development of rails. If you have other insights into this problem or suggestions for how to improve, please leave a comment!

Comments (3)

Book Review: Pro Drupal Development

Book Site | Sample Chapter: The Theme System | Table of Contents

Many of you are aware of my current total infatuation with Ruby, and that I’ve
used PHP for about 6 years and at one point decided I hated PHP…until, I
needed it for a quick one-off page and then realized that PHP had its place.
Then again, I was totally frustrated with Ruby when making my moms bakery site and then turned to Drupal and Gallery (another fine PHP project), which saved my bacon and I got a website and photo gallery up in a weekend. So, PHP and I have had our moments but I’m not abandoning it!

Drupal powers some big sites, its not just for joe smoe’s blog. This is an interesting page about Is Drupal Right For You? and if you are wondering if its something that would even work for you.

I was excited to get my hands on a review copy of Pro Drupal Development. Its no
secret that coders hate documentation and Drupal has one of the most complete
online documentation I’ve seen for an Open Source project, but its almost too
hard to find what you need amongst so much. The Pro Drupal Book is a godsend for
the drupal programmer, new and experienced alike. I wish it was written a year
ago!

The book starts off with a quick overview of how Drupal is structured and
defines terms such as hooks, node and blocks in just 10 pages. Chapter 2 is a A
step-by-step tutorial with making a module. That is a great idea to start off
quickly writing code. It get the reader involved and hands on. I really tire of
books that have to start off with the history of the internet, html and how
things have evolved. Get to the code dangit!! Kudos to the Authors for that!
Chapter 3 gets into module specific settings, like how to get your module to
show up on the admin page and storing user settings that your module needs.

After you’ve had some experience with the code then the book goes into details
on the specific parts of Drupal:

  • Menu System
  • Databases
  • Users
  • Nodes
  • Themes
  • Blocks
  • Form API
  • Filter System
  • Searching and Indexing
  • Files
  • Taxonomy
  • Caching
  • Sessions
  • JQuery
  • Localization
  • Using XML-RPC

Drupal is a pretty amazing framework, when I read the code I say “why didn’t I think of that?” … the module and hook system is genius.

Then some more general topics:

  • Writing Secure code
  • Development Best Practices
  • Optimizing Drupal
  • Installation Profiles

One of the chapters I skipped ahead to read was The Form API. In my years of PHP
I’ve often tried to come up with a framework for doing forms and I wanted to see
how they did it. This chapter follows a tutorial style as well. The Form API
allows you to define fields, their label, their value, description. Some
frameworks take the template approach, where you hammer out your HTML. Some are
more configuration based like Drupal making a multi-dim array with keys and
values. I can see advantages to both. There is a hook function for validation
which allows you to write your validation checks.

PHP gets a bad wrap for security, partly because its pretty easy to learn
PHP and newbies don’t always realize what they are doing. There is a
chapter devoted to security and includes even some things I didn’t know about –
encoding mail headers. The Form API is very secure,  one thing it does is
check values that come from dropdowns were actually in the options and it wasn’t
something that the hacker made up.

Developer Best Practices are great for the new developer, it talks about using
cvs, tags, branches. It talks about how to create and apply patches (hint - you
can contribute back to drupal). That is awesome. Alot of open source projects
are like “HELP us, submit patches!” and the new user is left with uhhhhhh..how?

Caching is another interesting chapter. You will learn  how caching works
and how Drupal Core uses it. There is a Cache API that has methods for module
creators to make their modules faster.

JQuery … I am not sure if I like it or not, but its part of Drupal 5! I
skipped ahead to this chapter to see what its all about. There is a javascript
hook built into Drupal making it easy to add, thats pretty cool.

One thing I found lacking in the book is anything about Testing. There are few
pages on debugging and some modules to help with testing, but I would like to
see more. At least some talk about selenium, which is great for a site made with
any framework/cms.

Over all, Thanks APress for another great book!

Comments (1)

Get Er DONE!

Well — anyone who knows me know that I love ruby… and perl next, and PHP comes in last of my favorite development languages. So of course when mom needed a website for her Bakery, I was going to do it in Ruby! I spent time getting my environment setup… threw it in subversion… setup a staging area on my server.. got some verbage, some photos and then finaly ready to hammer out the code.

I needed about 10 or so pages and an image gallery that would be easy to post photos to. I did a site a few years ago with Rails and file_column plugin and it was fantastic. I did it in 2 days and it was wonderful.

I am not quite sure what the problem was but I couldn’t get file_column to work for nothing! .. so I tried acts_as_attachment… no luck there either. i was using rails 1.2.2 on live and staging… i searched and searched on the errors I was having and no luck! PHOOOOOOOOEEEEYY! I decided to just use Dreamweaver Templates (which actually are quite nice for small sites if you want to avoid programming) and make a static site… to get at least SOMETHING UP!

I decided to use PHP’s Gallery to store photos, at least for the admin side. I had used it before and it has a fantastic java app called Gallery Remote that you run on your PC to upload photos. That’s easy. Then for some reason I thought - hey wonder whats up with Drupal? I went … I looked… hmm. So I downloaded it to my localhost and installed. Very nice! So I put it up on my moms site and configured it, added a module to connect to gallery — and within 2 days I was done. Sure I needed a few more little things, but that was way more then I could ever hope to accomplish with my homegrown site in rails.

So rails, I’m a little miffed at you. i wonder, are there too many cooks in the kitchen? are you getting kinda too big too fast? or am I just lazy and didn’t want to write a file upload, image resizing code myself? I didn’t use scaffolding for some parts and did TDD as I went along! I dunno…

I’ve always liked the Gallery codebase, its fantastic and Drupal as well. I’ve used and developed custom code for both. With good codebases, PHP doesn’t seem so bad. I might actually see if I can contribute to those projects.

I’m not saying that Rails == Drupal … using Rails would have certainly been more coding on my part, but the end result is about the same. What I wanted to do with Rails, Drupal had most of it done for me.

BTW, if you want to see the bakery site, check it out. We have a new logo and color scheme in the works!

Comments

Syntax .. Smintax ..

Last week I was called upon to write some Perl - something I haven't done since last fall. It was funny how as I was working on it, it started to come back to me. So I thought it'd be fun to compare my favorite languages a little bit:

Looks at how arrays are defined and the way I like to loop through them:

PERL:
  1. @books = ('Learning Perl', 'Advanced Perl Programming', 'Perl Best Practices');
  2.  
  3. foreach $book(@books) {
  4.   print "* $book\n";
  5. }

Foreach is actually an alias for "For" and some prefer that in this use because it makes it more readable. It also looks like php...

Now, for the language that has consumed my time the past 6 months...

PHP:
  1. $books = array('Pro PHP Security','PHP Cookbook','Pro PHP XML and Web Services');
  2.  
  3. foreach($books as $book) {
  4.   print "* $book\n";
  5. }

And.... here's ruby:

RUBY:
  1. books = ['Programming Ruby','Ruby Cookbook','Mr. Neighborly\'s Humble Little Ruby Book']
  2.  
  3. for book in books
  4.   print "* #{book}\n"
  5. end

I never actually used the for loop like that with an array, I usually use this version which is what the parser converts it to anyways:

RUBY:
  1. books.each do |book|
  2.   print "* #{book}\n"
  3. end

Fun stuff :)

Comments (1)

PHP User Group Meeting Report

I presented some features of XDebug at the February PHP User Group meeting, which we discovered is 5 days away from our "10 year birthday" of the group! If I had realized it earlier, I would have brought a cake!

I talked about installing XDebug, which was sort of a pain at first since this was the first time I installed a PECL module. With debian/ubuntu I found I had to have make, build-essentials, php5-dev (or php4-dev) from the software repository for my linux distribution and of course, Pear needed to be installed. Then I was able to do "pecl install xdebug-beta" and it would install, so it was not so hard once you had the linux stuff installed. Then I had to put "extension=xdebug.so" in my php.ini file. Then, you had to go about adding a config section in your php.ini to turn on the features of XDebug you want to use - debugging (things like glorified var_dump), profiling, and working with a debugging server.

I showed a few of the debugging features like the var_dump like functions, but i mainly focused on profiling and looking at the results both in WinCacheGrinde (windoze) and kCacheGrind (linux). I compared a PHP4 script using pear's xmlserializer class and PHP5 using the built-in xml extension. The differences were quite noticeable and I may write up something in detail on this at a later time.

Then Rich, the group leader, stole Rasmus's talk about Getting Rich with PHP5 and showed the slides and talked about how Rasmus used a similar profiling tool and was able to increasingly narrow down the number of servers needed for a particular application. Profiling is a great way to see what parts of your application are worth optimizing. The slides showed output from a different, but similar tool to what I demonstrated and the two talks were great together!

Then, we come to the fun part.. the random sharing of knowledge... I give a very brief demo of selenium before most people got there... I showed the glories of using VMWare player to use one of the ready-made ubuntu machines out there... we talked about Drupal ... opensource .. a bit about upcoming PHP-Tek ... it was a good time :)

Rich asked for volunteers for helping with the website -- any takers? :)

Comments

Feb Ruby Meeting Report - Capistrano and Starfish

I came to the meeting knowing a bit about capistrano and nothing about starfish and left with a firms grasp of basic concepts of both!
In short:

  • Capistrano - A tool for deploying actions on multiple servers. Not necessarily for Rails and you don't need Ruby on the deployment servers! Presented by Michael H Buselli
  • Starfish - Distributed programming in Ruby. Presented by Peter Chan

In Long(er):

Capistrano
Like Rails, this tool relies on convention over configuration and makes some assumptions about your environment such as Rails, Subversion, Apache 1.x and FastCGI. Of course you can override some of these assumptions and even use it with PHP and CVS (yikes). Future versions will be completely separate from Rails. I know people who stiffen at any mention of Rails, but really.. this is how tools are born, out of a need. This one so happened to be a need by Rails developers and thus it makes sense it would be naturally easier to use to deploy a rails site.
Commands
The basic commands are run, sudo (run as root), put, delete, render (returns output from erb template) and get. In addition you can add your own commands.

Tasks:
You group commands similar to a batch file or shell script. And interesting thing is if your task is called "say_hello" ... you can also have a task "before_say_hello" and "after_say_hello" that will run before and after respectively. This might be useful for making "changes" to some of the standard tasks that will do any preparation or cleanup without having to hack the code. The question was asked if you could call "before_before_say_hello" and yes, recursive calls that that do work..though I think it could get pretty confusing!

Roles:
Machines are grouped by roles, such as "web", "db" and you can have multiple machines in those roles. The db role is unique in that you specify one as primary, because thats where the migrations are run (then I'm guessing that the database is just copied to the other database servers?).
Putting them together:
You can specify on the tasks which machine role it is to be used for such as:

task :say_hello_to_webservers, :roles => :web do
run 'echo "hello world" '
end

Anyways thats the basics as I understood. Please correct me if I am off base.
Link to presentation and resources: http://www.cosinewave.net/ruby/cap

Here's a blog posting that describes how to use Capistrano with Perl or PHP which I bookmarked some time ago, it may be a little out of date but probably has some good information still.
---

Then we had a brief moment of fun as we watched this (which is no joke!)

Erlang - http://tinyurl.com/ytgp27

---

Starfish
This was interesting as I have never done distributed programming or had a need too, but I'm always wondering how things work. The presenter said that he thinks this is one of Google's secret weapons in making things load faster.
The starfish file consists of two sections -- the server and the client. The server section describes the process and the client section describes the output. Once that is set, you run the starfish program and the first time you run it, it starts a client and a server. To start another process, run the starfish command again and this time it sees there's a server already started and then just starts up another client.
ex:
starfish find_primes.rb #starts server, client
starfish find_primes.rb #sttarts another client
Pretty neat, I had to leave before the end of this talk since I have such a long commute home but I got the jist of what starfish was and know where to look if I need distributed programming in the future!

Links to his demo files: http://oaktop.com/go/starfish/

---

Live in Chicago? Join Chirb, the Chicago Ruby group. Can't make it downtown? There some individuals starting meetings in the burbs, join the mailing list for details!

Comments

Perl, PHP and Ruby oh my!

This week, I attended the Perl and PHP meetings. Though they were late nights for me, they were good meetings and worth the loss of sleep.

Perl - Catalyst
Catalyst is a MVC framework for Perl. I like Rails -- alot, but not just cuz its Rails because its Ruby. I love Ruby. Perl I really like also, but I'm not so sure its a fantastic language to develop for the web. After seeing the presentation I say, thats nice... but.. I will probably use Rails if I were to use a MVC framework. I'm wearing my Perl shirt today!

By the way -- if you are south of chicago there's a Perl meet up in Tinley Park on Jan 24, 7pm

Caribou Coffee
16205 Harlem Ave
Tinley Park, IL 60477
(708) 444-0478
PHP - Firebug
We didn't really have any topics planned out for this one -- but we had some volunteers. Peter did a good overview of the indispensable tool for debugging javascript, css and HTML. Unfortunately, it only works in Firebug, but there is an add javascript to use with IE and get a few features to make your life a bit more bearable in IE. Larry jumped in and showed some Javascript Debugging. Next month at PHP -- profiling fest with XDebug, and Valgrind. Should be interesting!

Ruby - no love!
I've been moping about this week about Ruby... I haven't done it in about 4-5 months since being back in PHP-Land? I'm having withdrawals. It all started after I read this article Technologies of the Year 2006 (BTW -I have done all of them!). I was thinking ..awwww... Rails.... and I picked up my Rails book at work that I look at when I am fed up with PHP, just to cheer me up a bit. I skipped the last Ruby meeting, at the last minute the buddies that I thought would go with me backed out.. and.. well.. I was pretty tired and the topic of "Environments" made me think it would be a mac-love fest. I used to want a mac laptop, but... I've decided to stick with the PC environment since I am happiest in Ubuntu. I am determined, buddies or no buddies, to go to the next meeting which should prove more interesting. The Chicago Ruby list has been buzzing with topics and volunteers for presentations. There's even an outbreak of smaller meetups in other areas of the city.. North, South... fun times.

Comments (1)

Book Review: PHP Hacks

Book Review: PHP Hacks by Jack D. Herrington, published by O'Reilly

I had borrowed a Perl Hacks book from and friend and really liked it, it was great! It had a lot of practical things as well as some fun things. I expected the same from PHP Hacks and I was not disappointed!

Here’s the table of contents:
http://www.oreilly.com/catalog/phphks/toc.html

O’Reilly also has some sample hacks:
http://www.oreilly.com/catalog/phphks/chapter/index.html

Here's some that I found interesting:

The Practical Stuff
Breadcrumbs
Not familiar with the term? Check it out. I think this hack may get you started on a breadcrumb function/method. In the end they suggest a xml file to show which page urls were parents of which. The way I've done this before was I had a class for each major section, and sub section, and had a method ->addCrumb($label, $url) which I had in the constructor…and the subsections of course would call the parent contructor and it kept the breadcrumb hierarchy intact. But hey, that’s the fun of programming - different ways to do the same thing to meet different needs! Definitely a good hack to get the juices flowing!

Building Lightweight HTML Graphs
Don't want to use flash to display a graph? use PHP to figure out the ratios and give you a width and use a table. I've done something similar by figuring out the width of a div, making the background a color… and I have used 1x1 pixel images that have been stretched to a certain width and height. It’s a very lightweight download for your user, that’s for sure! Later in the book, there's a hack for creating a Dynamic HTML Graph that will change without reloading the page.

Put an Interactive Spreadsheet on your Page
This one is so cool -- you need to read about it in the book yourself! Lets says, move over Google Spreadsheets! We can do it too!

Create Link Graphs
I call these Tag Clouds, not sure why they call them Link Graphs here in the book -- probably, tag clouds has been copy written by some Web 2.0 smartass. Here I am, sue me! This is a unique and visual way to show the popularity of certain words in a group. Rather than a numbered list, this is visual. I had this discussion not too long ago with a group and sadly, most of them didn't get it. I think if I actually used this sort of technique on a page, I'd include a "What's this?" link or an alternative view.

Create Dynamic Database Objects
This was very interesting to me because I love Active Record in Rails. This relies on some of the magic of PHP5 to work, probably this is not going to be the best performance code but really -- is anything easy the fastest?

Generating CRUD Database Code
Similar to previous, but a create-once and go method, this hack will read from a xml file and create CRUD objects for maintaining your database. These will probably be faster then the previous one -but you'd have to run this script or update manually when your schema updates. Some people hate code generation - some don't. Pear's DB_DataObject is a similar concept.

There are a few other nifty database hacks making this my favorite section of the book!

Turn any Object into an Array
Using foreach is my default iteration function and using the PHP 5 iteration interface on any object to give it that functionality is awesome. This is one of the most practical design patterns (other than the other favorite: singleton) that I talk about to people who ask me - what are design patterns and why should I care? Speaking of design patterns - tired of reading a design pattern book and trying to figure out the smalltalk or java code? the design pattern section of the book has diagrams and sample code that you can understand.

Fun Stuff

Build a DHTML Binary Clock
What is that you say? Take a look at ThinkGeek and learn how to make something similar in DHTML. Yes, this is not practical but its fun. Something fun is always a great way to get excited about programming and enjoy yourself.

Generate Your Unit Tests
I put this in the fun section because testing IS fun - I love it. One of the things people (normal people, not wacky test freaks like me) complain about testing is - no time! Here's a hack that will let you put your test in a comment and running this script on it will pull out those comments and write your test for you. Nifty!

Build GUI Interfaces with GTK
I've always wanted to try something with GTK, but never have. This hack shows you how to build a regex expression tool to test and play around with regex. I've had programs like this and they are darn handy when you want to do a quick check. So, after you've had your fun building this -- its practical as well.

Send RSS feeds to your IM Application using Jabber
Depending on your use, this may in fact be practical but I think its rather fun. I have not figured out a use for this myself but the book uses it to send weather forecasts to your IM client. Neat-o

What I didn't like about this book
not too much in the way of checking input variables. I know, probably they "leave that as exercise to the reader" but noobs and experts alike need to get this ingrained into the head by default. So I suggest also picking up Essential PHP Security

What I like
ER diagrams and control flow - Nice!
Handy size

I've only touched on a few of the great hacks and there are many more I also like, but didn't really want to give away the entire book! I suggest you pick it up and keep it handy. Next time you are bored -- flip though and find something interesting!

Comments

Zend Conference, part 2

Its been over a week, seems like I just got back!

Since then, I've done alot of thinking about the PHP Community. Last fall I was ready to throw in the towel on PHP and started studying Perl and Ruby. I've learned alot and its made me think about problems in terms of algorithms not "which php function do I use to do this" ... and being at the Zend Conference has renewed some PHP hope -- with the PHP Unit testing, Selenium integration, XDebug and the focus on design patterns and appropriate use of OOP, I think I'll put my energies into PHP for awhile.

The official start date of the conference was Monday and started with a presentation by Zend about the state of PHP. It was this that made me think, hey ... PHP is making good progress. Zend announced quite a few things. Here's what stood out to me

ZendBox - PHP5 Dedicated server hosting, with the latest Zend software. One of the problems right now is finding hosts that will have the latest PHP software. Its probably been one of the drawbacks to getting into PHP5.

Microsoft - They announced a partership with Microsoft to improve the performance of PHP on IIS. At first I was like ahhhhhhhh... then I was like, ohhh, if PHP performs well on windows it will take more marketshare from ASP.

IBM - They announced another partnership with IBM who is now shipping Zend products on their new 'System i' line of servers.

Here's an interview with Andi about these new partnerships.

Another thing I noticed, and in contrast with YAPC (Yet Another Perl Conference) there is not the bitterness and jealously over Rails success in PHP as is in Perl. There was a few comments about Rails at the PHP conference, but if anything, PHP has been inspired by Rails. Anyways, interesting contrast. The PHP community doesn't see Rails as much of a threat.

Comments

Chicago PHP User Group

We will have a special speaker in October and in order to accommodate his
schedule we are changing the regular meeting day from Wednesday, Oct
18 to Monday Oct 16th.

Same time and place!
Time: 6pm
Place: Info.com 150 N Michigan, 28th floor
If you will be arriving PAST 7pm, give me your name and I'll tell Security
to let you up.

Here's the topic:
--

What's possible in a post Web 2.0 world? Innovation continues at a
mind-bending pace, and this presentation will showcase some
thought-provoking new directions that Web Services are headed in. The
presentation will provide an overview of Amazon Web Services, and feature a
demonstration showing how developers are able to easily utilize on-demand
server capacity.

Amazon spent ten years and over $1 billion developing a world-class
technology and content platform that powers Amazon web sites for millions of
customers every day. Most people think "Amazon.com" when they hear the word;
however developers are excited to learn that there is a separate technology
arm of the company, known as Amazon Web Services or AWS. Using AWS,
developers can build software applications leveraging the same robust,
scalable, and reliable technology that powers Amazon's retail business. AWS
has now launched eleven services with open API's for developers to build
applications, with the result that over 160,000 developers have registered
on Amazon's developer site to create applications based on these services..

Comments