Posts Tagged ‘php’

Stay away from Bloated Class Methods, actually, bloated ANYTHING!

March 15th, 2008

Why are there so many examples of bloated PHP methods out there?! I see a lot of bloated methods being used in examples through sites like phpclasses.org and a very large chunk of a project I’ve recently picked up which a few developers have worked on. Guys! remember! There is no good reason to bloat your methods to over 10-20 lines long and on the rare occasions that you do exceed 10 – 20 lines it would be more the exception to the rule as opposed to being the rule itself!

Let’s not forget what classes and its methods are supposed to be: Classes are blueprints for specialist components and their methods are singular actions which address one to a few (2 – 3 at most!) procedures at a time whether it be through the use of other classes or just through simple logic!

» Read more: Stay away from Bloated Class Methods, actually, bloated ANYTHING!

Execute Linux commands in PHP by using backticks

January 13th, 2008

You can execute linux commands within a php script – all you have to do is put the command line in backticks (`).

Recently, I had to upload an archive to a server which did not allow ssh. For files that I need to transfer, I just package them up in a neat archive and transfer the archive file via scp. Not having ssh access however, I uploaded the archive file via ftp and created a script which extracts the file the file looks like the following:
» Read more: Execute Linux commands in PHP by using backticks

PHP HTTPRequest Class

January 10th, 2008

Note: This is is a quick example of a very basic HTTP Request Class in PHP. This would be a nice, practical introduction to classes for someone who has been looking for a place to start.

Problem: By default, request data in PHP is handled by using Super Globals ($_POST, $_GET, $_COOKIES). Globals are bad for several reasons (outside the scope of this example google here). In an effort to remedy this, we need a way to encapsulate the request data into an object. By doing this, we are centralising access to request data through the one channel – the HTTP Request object.

Class Name:HTTPRequest

Responsibilities:

  • Store “request data” in GPC order.
  • Store additional “request data” outside of the super globals referenced by a key.
  • Read stored “request data” by referencing a key.
  • Internally clean request data by handling magic_quotes_gpc and then adding slashes.
  • Allow access to data stored in GET, POST and COOKIE super globals.

Collaborators: Any – the request object is used by any other class or function that requires access to the request data.

» Read more: PHP HTTPRequest Class

Classes & Examples Sub-Category

December 6th, 2007

I’ve created a “Classes & Examples” sub category for PHP. This category will be different from actual Design Patterns in a way that these will be more practical examples which you can modify and use in your own code base (all I ask is that you leave my name somewhere in the credits).

The Posts I will be making in the “Classes & Examples” Sub-Category will be formatted in the following way:

» Read more: Classes & Examples Sub-Category

NIH Syndrome in PHP – CURE YOURSELVES YOU HEATHENS!

December 5th, 2007

After browsing through some other web developer’s websites and speaking to other developers, I think it’s a shame that there’s so much of the NIH Syndrome going on in PHP.

I find that PHP is one of the languages where NIH syndrome is rampant, or at least it has been, nowadays people are slowly starting to practicing some “reusage”. This is a good thing.

» Read more: NIH Syndrome in PHP – CURE YOURSELVES YOU HEATHENS!