PHP Articles

Do You Really Need a Framework for Writing PHP?

PHP is a server-side scripting language used all over the world for general purposes. Earlier, PHP used to be the short for Personal Home Page. It is now known as ‘PHP: Hypertext Processor’. It is very helpful for web development and can be blended into HTML too.

Moreover, it is easy to learn. PHP includes many functions without the need for namespace importing. You can decide whether to write object oriented code or not. The syntax is also similar with weakly typed variables.

The simplicity in using PHP sometimes acts against it. Since there are very few coding restrictions, developers tend to write a bad code. The answer to this is definitely to use a framework. There are various PHP Frameworks available today like Zend Framework, CakePHP Framework and CodeIgniter. They provide a strong organization for your application and follow the commonly used MVC pattern.

Some of the other advantages of using a framework are:

  1. MVC structured code:MVC or Model- View- Controller is a well-known design pattern that demarcates the database and business logic from the presentation layer. This results in simple, clean and understandable code.
  2. Maintaining the coding standards:The MVC code is easy to write and also follows the code restrictions. Due to a uniform and standard coding, things are simpler and bugs can be resolved faster. This will save you a lot of worry and it will be easier to find out where the problem lies.
  3. Attractive URLs:Almost all these frameworks make sure that your URLs look attractive and easy to access. Apache takes care of requests for a particular URL, which further invokes the central framework object. The end-users can only see the URL pasted on their browser, and cannot make out that you are using PHP for the same.

  4. Get all the help you need:When you use a PHP framework, you no longer need to manually enter all the validation criteria for forms etc. The only thing you should do is instantiating a new form validator object and instruct it as you wish. It will handle the rest.
  5. Finish coding faster:Frameworks help the developers to write code in lesser time than usual. This leaves them with some extra time for themselves.All said and done, there is always the option of writing your own framework for your application. However, when you have something like CakePHP doing a lot of nifty things for you or a Zend that provides great functionality, you can select one depending on your requirements. So just choose a framework and start coding!
no-nonsense-xml-web-development-with-php-new No Nonsense XML Web Development with PHP NEW
US $33.34
Auction Ends: Thursday May-17-2012 23:10:54 PDT
  | Watch this Item
dreamweaver-mx-php-web-development-by-bruno-mairlot-g Dreamweaver MX: PHP Web Development by Bruno Mairlot, G
US $4.46
Auction Ends: Thursday May-17-2012 23:37:43 PDT
  | Watch this Item
php-programming-for-windows-landmark-new-riders-andrew-stopford-acceptable PHP Programming for Windows (Landmark (New Riders)), Andrew Stopford, Acceptable
US $1.00
Auction Ends: Thursday May-17-2012 23:38:37 PDT
  | Watch this Item
stretched-huge-hand-painted-art-deco-cherry-blossom-flowers-oil-paintings-php513 Stretched Huge Hand-Painted Art Deco Cherry Blossom Flowers Oil Paintings Php513
US $169.99
Auction Ends: Thursday May-17-2012 23:44:12 PDT
  | Watch this Item

Zend Framework vs CakePHP Framework

The Zend Framework, developed by Zend Technologies is an open-source, object oriented web-application framework implemented in PHP 5. It is widely known as ZF and is developed with the purpose of making things easier for PHP developers and supporting best practices at the same time. CakePHP too, is an open-source web application framework used for creating web applications written in PHP. It is developed by Cake Software Foundation, Inc. It is written in PHP and is based on the model of Ruby on Rails.

Zend Framework has easy methods of licensing with the new BSD license and a swift and well-tested code base that your business can rely upon. It makes use of commonly available APIs from well known vendors like Google, Amazon, Yahoo!, Flickr and API providers and catalogers such as StrikeIron and Programmable Web.

ZF offers a simple component library to provide 80 percent of the functionality mostly needed by developers. The rest of the 20 percent can be altered as per your requirements of the business. This can be done since it has a flexible architecture and you do not need any configuration file to get started with. Due to this, training costs are lessened and the product reaches the market sooner. Moreover, Zend Framework also offers:

  • AJAX support through JSON, which offers ease-of-use of the framework.
  • The PHP version of the Lucene Search engine.
  • Simple access to data formats needed in Web 2.0 applications.
  • The ideal destination to utilize and publish web services.
  • Top quality, object-oriented PHP 5 class library.

CakePHP is a rapid development framework for PHP that offers a flexible architecture for development, maintenance and deployment of applications. It uses the usual design patterns like MVC and ORM within the convention over configuration paradigm that helps lower development expenses and does not need the developers to write a lot of code. CakePHP boasts of clients like Mozilla Addons, Scratch by MIT, Yale Daily News, Onion Store and MapMe to name a few.

Just like ZF, CakePHP does not require configuration. It is very simple to use. The company has a user friendly community called #cakephp on IRC that helps new users to begin. It is distributed under the MIT License and promises Best Practices like security, authentication and session handling. It has an object-oriented approach to keep you at ease.

Overall, both these frameworks are good to use as per your requirements. CakePHP is suitable for developers new to MVC and those who require stricter conventions. ZF is a choice for those who need better control over the application design and built-in support for renowned Web services. Both the frameworks are developing fast and the choice is entirely yours.


Twitter Tag Cloud Service

The past few months I have been using Twitter a lot.  I’m trying to find and follow people that are heavily involved with PHP or at least providing knowledgeable Tweets about PHP.  I guess you could say that I am always open to new networking opportunities within the PHP community. :)

If you don’t know what Twitter is, well then you aren’t keeping up with your current events very well.  I’m not going to use this post explaining what it is more than a mini-blogging service.  If you don’t know, check their site out.  I provided a link above.

The problem I was having was actually judging whether or not it would be worth my time following their tweets. I mean, I follow someone that is really respected in a certain field, only to find out that they tweet about their new puppy they just got more than anything. So I made a PHP script that generates a “Twitter Tag Cloud.”  Using this, you can generate your own Twitter tag cloud to put on your personal site to display the top keywords that you have been tweeting about.

I also provide a REST service so that you don’t have to upload or play with any code yourself other than accessing the BrownPHP Twitter Tag Cloud service. I’ll show you more about that later. Or you can just use the textbox I have here to check out your own Twitter Cloud or a friends.

This is helpful to judge what the person tweets about the most.  The more the Twitter-er Tweets about a certain keyword, the larger it appears. Like if you would look at mine.  One of the large keywords is “informative.”  I also run a site called, Informative Post, and I tweet about it often, so it makes sense that this would be the largest keyword.

View Your Twitter Tag Cloud Now

Try it yourself! Type in a twitter username to see what that twitter-er tweets about more often:


Pretty cool, huh?  At the bottom of this, I provide a link where you can download the PHP files from.  I am also providing this as a REST service.

REST Twitter Tag Cloud Service

Parameters:

  • user (required)
    • Twitter Username
  • history (optional)
    • Number of past status updates to look for
    • Defaults to 100
    • Max is 200
  • size (optional)
    • Number of Keywords to return
    • Defaults to 50
    • Max is 75

Example REST call to get my (dlbrown06) Twitter tag cloud:
http://www.brownphp.com/rest/twittertagcloud.php?user=dlbrown06
Response:

<div id="twitterTagCloud"><a class="size2 tag" title="dlbrown06's Tweets Covering money" rel="nofollow" href="http://search.twitter.com/search?q=+money+from%3Adlbrown06" target="_blank">money</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering taking" rel="nofollow" href="http://search.twitter.com/search?q=+taking+from%3Adlbrown06" target="_blank">taking</a>
<a class="size1 tag" title="dlbrown06's Tweets Covering networking" rel="nofollow" href="http://search.twitter.com/search?q=+networking+from%3Adlbrown06" target="_blank">networking</a>
<a class="size1 tag" title="dlbrown06's Tweets Covering social" rel="nofollow" href="http://search.twitter.com/search?q=+social+from%3Adlbrown06" target="_blank">social</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering would" rel="nofollow" href="http://search.twitter.com/search?q=+would+from%3Adlbrown06" target="_blank">would</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering children" rel="nofollow" href="http://search.twitter.com/search?q=+children+from%3Adlbrown06" target="_blank">children</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering account" rel="nofollow" href="http://search.twitter.com/search?q=+account+from%3Adlbrown06" target="_blank">account</a>
<a class="size4 tag" title="dlbrown06's Tweets Covering working" rel="nofollow" href="http://search.twitter.com/search?q=+working+from%3Adlbrown06" target="_blank">working</a>
<a class="size3 tag" title="dlbrown06's Tweets Covering article" rel="nofollow" href="http://search.twitter.com/search?q=+article+from%3Adlbrown06" target="_blank">article</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering start" rel="nofollow" href="http://search.twitter.com/search?q=+start+from%3Adlbrown06" target="_blank">start</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering currently" rel="nofollow" href="http://search.twitter.com/search?q=+currently+from%3Adlbrown06" target="_blank">currently</a>
<a class="size1 tag" title="dlbrown06's Tweets Covering making" rel="nofollow" href="http://search.twitter.com/search?q=+making+from%3Adlbrown06" target="_blank">making</a>
<a class="size4 tag" title="dlbrown06's Tweets Covering online" rel="nofollow" href="http://search.twitter.com/search?q=+online+from%3Adlbrown06" target="_blank">online</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering twitter" rel="nofollow" href="http://search.twitter.com/search?q=+twitter+from%3Adlbrown06" target="_blank">twitter</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering morning" rel="nofollow" href="http://search.twitter.com/search?q=+morning+from%3Adlbrown06" target="_blank">morning</a>
<a class="size1 tag" title="dlbrown06's Tweets Covering sites" rel="nofollow" href="http://search.twitter.com/search?q=+sites+from%3Adlbrown06" target="_blank">sites</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering right" rel="nofollow" href="http://search.twitter.com/search?q=+right+from%3Adlbrown06" target="_blank">right</a>
<a class="size5 tag" title="dlbrown06's Tweets Covering informative" rel="nofollow" href="http://search.twitter.com/search?q=+informative+from%3Adlbrown06" target="_blank">informative</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering hosting" rel="nofollow" href="http://search.twitter.com/search?q=+hosting+from%3Adlbrown06" target="_blank">hosting</a>
<a class="size1 tag" title="dlbrown06's Tweets Covering until" rel="nofollow" href="http://search.twitter.com/search?q=+until+from%3Adlbrown06" target="_blank">until</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering people" rel="nofollow" href="http://search.twitter.com/search?q=+people+from%3Adlbrown06" target="_blank">people</a>
<a class="size1 tag" title="dlbrown06's Tweets Covering looking" rel="nofollow" href="http://search.twitter.com/search?q=+looking+from%3Adlbrown06" target="_blank">looking</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering starting" rel="nofollow" href="http://search.twitter.com/search?q=+starting+from%3Adlbrown06" target="_blank">starting</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering really" rel="nofollow" href="http://search.twitter.com/search?q=+really+from%3Adlbrown06" target="_blank">really</a>
<a class="size3 tag" title="dlbrown06's Tweets Covering diablo" rel="nofollow" href="http://search.twitter.com/search?q=+diablo+from%3Adlbrown06" target="_blank">diablo</a></div>

Then you can use CSS to format the cloud however you like!

Download the Twitter Tag Cloud Source Code:

If you would like to use this source code yourself, you are free to do so.  All I ask is that you provide a link to this site, or somehow recognize the work that I put into it somehow.  I would love to see what kind of mashups that the PHP community could do with this source code. Enjoy!

Addendum (05/12/10):

This code has been added to the BrownPHP Code Repository if you would like to download it. You can go straight to the download by clicking the BrownPHP Twitter Class


CNET’s 404 Podcast Twitter Tag Cloud

If any of you haven’t heard of CNET’s 404 podcast, you are missing some serious entertainment. I have been listening to them for about a year now, and haven’t missed a podcast since. The guys at the 404 (Randell, Jeff, Wilson, and the new and improved Justin) just started Twittering, or at least telling everyone else that they are twittering.

I decided to make a little php script that monitors their Twitter RSS feeds, and from them, generates a tag cloud from their tweets. From listening to their podcast, I thought it would be pretty interesting to get a quick summary of what they are tweeting about more often.

I see a “hooters” tag in Jeff’s cloud, and I see a “balls” tag in Wilson’s…? Coincidence?

404 Tag Cloud

Jeff’s Tag Cloud

Wilson’s Tag Cloud

Justin’s Tag Cloud

If any of you are interested in the PHP Twitter RSS Tag Cloud Generator, let me know and I can send it to you.


Zend_Cache is Saving me Money!

Let me tell you a little story.  My latest site I have been working on for some time was written using the Zend Framework.  Since it was launched in Oct 07, it has been growing rapidly enough where I am now starting to feel growing pains.

One morning I went into the site to check some analytics, and my site was displaying a 403 Forbidden error!  I suddenly got a cold nervous sweat down the back of my spine.  Why was it forbidden?  It was fine when I checked it out last night?!  So I tried to login to my FTP account.  No go!  I I thought a hacker has some how blocked me out of my own account. [Exit dramatic build up].

I come to find out that my hosting company revoked my access to the folder that I was using for the domain.  I called them up immediately after I noticed that.  They responded by telling me that on a shared server, they limit their clients to use only 2% of the servers resources before they shut them down.  My site was using 30%. They were then forced to shut the site down without notice, because it was most likely affecting the performance of the other sites running from that shared server.

After some negotiating and explaining to them that I was going to come up with a solution, they temporarily gave me back access to the folder so that I could diagnose further what was going on.  The first thing that I did was looked through the server logs.  Because of my unfortunate neglect, the server log file was freaking enormous bigger than normal.

Nearly all of the errors I was getting were:

[03-Jul-2008 09:41:19] PHP Fatal error:  Uncaught exception 'Zend_Db_Adapter_Exception' with message 'SQLSTATE[42000] [1203] User blank already has more than 'max_user_connections' active connections' in blah blah blah

I’ll admit, I was in a huge hurry to get this project done, so I wasn’t thinking about the long tern effects.  Needless to say, I wasn’t caching my MySQL query results. I know, tisk tisk. Once I discovered this, I added this private method to my classes:


private function loadCache()
{
$frontendOptions = array(
'lifetime' => 7200, // cache lifetime of 2 hours
'automatic_serialization' =>
true);
$backendOptions = array(
'cache_dir' => './cache/' // Directory where to put the cache files
); // getting a Zend_Cache_Core object
return Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
}

Then in each of my classes where I needed to cache my query results, I implemented the above method. Here is an example:


class IndexController extends Zend_Controller_Action {
/**
* Index Action
*
*/
public function indexAction() {
$db = new SomeDB;
$cache = $this->loadCache();
if (!$results = $cache->load('cache_variable')) {
$results = $db->fetchAll('columnName="whatIWant"');
$cache->save($results, 'cache_variable');
}
}
}

It’s pretty much as simple as that.  Once I did this, it dropped the server load tremendously, which now buys me more time from upgrading my hosting package.  About 10 minutes of coding just saved me about $50 a month in hosting fees!  That feels good.

Keep in mind that this is not the best approach to solving this problem.  If you too are using the Zend Framework in your own design.  It was be better to initialize the Zend_Cache in your bootstrap file.  Doing it the way I did above will force you to replicate the code method, loadCache, in all of your classes which I dont’ have to explain to you why this is inefficient, and just plain old bad OOP technique! :P


  • BrownPHP Tag Cloud

  • Copyright © 1996-2010 Brown PHP. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress

    Powered by Yahoo! Answers