Pages

Showing posts with label Web. Show all posts
Showing posts with label Web. Show all posts

Sunday, September 15, 2013

OOPS-Static keyword,Singleton Design Pattern and its implementation

Static keyword is useful in creating Singleton design pattern because in such a scenario changes made by 1 object to a variable is reflected in all the objects of that class in that request.

Here is my example code with Singleton Design Pattern.

What I was trying to do is I was trying a singleton object over different request's and was trying to see whether it maintains its state or not. And it doesn't I tried running this page from different browser's and I failed to retrieve data of previous request's or session's.

Singleton Pattern is just needed so that only one object access at a time a resource to avoid conflict's

Singleton's are used to write log's and database access,maintain configuration variable's.

And that is when I realized the importance of an Application Server over a Web Server where we can create application object's.

 <?php  
 session_start();  
 //Singleton class example  
 error_reporting(E_ALL);  
 final class UserInfo  
 {  
   static $user=null;  
   static $instance=NULL;  
   public static function Instance()  
   {  
     if (self::$instance === null) {  
       self::$instance = new UserInfo();  
     }  
     return self::$instance;  
   }  
   /*  
    * Shows the array  
    */  
   public static function showUser()  
   {  
     if(self::$user!=null)  
     {  
       foreach(self::$user as $key=>$value)  
       {  
         echo "<br/>Sessionid ".$key." is assigned to ".$value."<br/>";  
       }  
     }  
   }  
   /*  
    * Adds session Id and user to the array  
    */  
   public static function AddUser()  
   {  
     echo "<br/>";  
     echo "Object count is ==>";  
     echo $ucount=count(self::$user);  
     echo "<br/>";  
     if(self::$user===null)  
     {  
       self::$user=array();  
       self::$user[session_id()]="User ".($ucount+1);  
       //print_r(self::$user);  
     }  
     else   
     {  
       if(!(array_key_exists(session_id(),self::$user)))  
       {  
         self::$user[session_id()]="User ".$ucount+1;  
       }  
     }  
   }  
   private function __construct()  
   {  
   }  
 }  
 echo "My Session Id is :". session_id();  
 UserInfo::Instance()->AddUser();  
 $fact1= UserInfo::Instance();  
 $fact= UserInfo::Instance();  
 $fact1->AddUser();  
 $fact->showUser();  
 UserInfo::Instance()->showUser();  
 if($fact1===$fact)  
   echo "Matches";  
 /*  
  * ERROR LINE  
  */  
 //$fact3= new UserInfo();  
 ?>  

Output is:
 My Session Id is :p15snn5ebgn52ftsj0eb43e4n0  
 Object count is ==>0  
 Object count is ==>1  
 Sessionid p15snn5ebgn52ftsj0eb43e4n0 is assigned to User 1  
 Sessionid p15snn5ebgn52ftsj0eb43e4n0 is assigned to User 1  
 Matches  

As you can see above is mine singleton class with a private constructor.I have 2 static methods one to store data and other to show data

In first case I have added user name as value and corresponding session id as key to the array.
I have called my method in 2 way's directly via class and by an object.

In the second call count goes 1

Finally I call show user in 2 way's and the 2 object's matches as per the condition

Wednesday, March 27, 2013

PHP Web Programming and Business Benefits of PHP Development

Inception of PHP is almost two decade old, when Danish programmer Rasmus Lerdorf created PHP in 1995. Inspired by that, in year 1997, two Israeli programmers Andi Gutmans and Zeev Suraski rewrote the parser, creating the base for PHP 3 and also built the Zend engine which is still followed as the interpreter for PHP.

Influence Of PHP Across Web

Today, almost two decades after countless colossal modifications to codebase, more than 35% of web traffic is handled by PHP. Facts on Wikipedia say 75% of websites use PHP, which includes some giant domains such as Facebook, Wikipedia, Yahoo, Photobucket and many others. Amazingly, list doesn’t end here; world’s best blogging platform “WordPress” and other popular CMSs like Drupal and Joomla are built in php.
Above mentioned facts are enough to claim the dominancy of PHP even in the presence of killer web development technologies. But, what are the reasons, why php hits the mainstream enterprise and why it is forecasted to maintain its momentum even in the coming years.
Success of php web programming totally bets on advantages which it offers to its users. Not only PHP developers find it convenient, but even end-users claim it to be friendly. Several studies have concluded some common advantages offered by php development, and they are as follow.

Free For All “Its Open Source”

Under GPL (General Public License) PHP is freely available for use, along with its associated software’s like MySQL, Text Editors and Apache Server to reduce your overall development cost. With the aim to serve you better, php community offers technical support and is constantly sweating hard to improve the core functionalities of PHP.

Cross-Platform Compatibility

It exhibits high compatibility with all the major OS and servers for easy deployment across vivid platforms. PHP scripts can run across OS like Linux, Windows, Solaris, OpenBSD, Mac OSX and others. It also provides support for all leading servers such as Apache, IIS, iPlanet and so on.

Powers Equally – Either Small or Big Websites

Either small or giant, whatever are your business needs a php based business website can be easily developed in not time and in limited budget. Business and organizational websites, informative forums, CRM solutions, e-commerce websites, community websites, e-business and gigantic database driven sites can be easily developed using php.

Extensions to Expand Functionalities

Plethora of extensions and libraries are available across the web using which you can extend core functionalities of your website. Even custom created extensions can be included into source code of PHP.

Top Security – For Hacker Proof Website

It offers top level security that helps prevent malicious attacks. These security stages can be easily attuned in the .ini file.

Veteran Developers to Execute Your Plans

You can hire PHP programmers/developers from reputed web development company more easily than any other language programmers. Just follow simple tips to hire veteran developers and stay away from worries.
In the end, if you are in the mood to add dynamic content to your project, then use of PHP is highly recommended. It free, easy to understand and integrates almost all the functionalities as per your needs. There are tons of php developers available on the net. Please don’t hesitate to add anything if I have missed some other great PHP benefits. Feel free to share your thoughts with other readers.