Thursday, 9 February 2012

compressed file in PHP

Better to compress files in PHP. It reduces size of the page and speed up loading of the page. 

Factory Pattern PHP

<?php

/*
The Factory pattern allows for the instantiation of objects at runtime. It is called a Factory Pattern since it is responsible for "manufacturing" an object. A Parameterized Factory receives the name of the class to instantiate as argument.

*/


interface IUser
{
  function getName();
}

class User implements IUser
{
  public function __construct($id) { }

  public function getName()
  {
    return "Jack";
  }
}

class UserFactory
{
  public static function Create($id)
  {
    return new User($id);
  }
}

$uo = UserFactory::Create(1);
echo($uo->getName()."\n");
?>

Single Design Pattern Example in PHP

<?PHP
/*
The Singleton ensures that there can be only one instance of a Class and provides a global access point to that instance. Singleton is a "Gang of Four" Creational Pattern.

The Singleton pattern is often implemented in Database Classes, Loggers, Front Controllers or Request and Response objects.

*/


    class Singleton
    {
        static private $instance;
        protected function __construct() {}
        final private function __clone() {}
       
        static function getInstance()
        {
            if(!self::$instance)
                self::$instance= new Singleton();
                return self::$instance;
        }
    }
   
    $a = Singleton::getInstance();
    $a->id=1;
    $b = Singleton::getInstance();
    print $b->id."\n";

?>

PHP Session (Diff b/w Cookie and Session) Sample/Example

<?PHP
/*
Cookies:
A cookie is a text-only string that takes a place in the memory of user’s browser.
The main difference between cookies and sessions is that cookies are stored in the user's browser, and sessions are not.


Session:
When you are working with an application, you open it, do some changes and then you close it. This is much like a Session.

A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping items, etc). However, session information is temporary and will be deleted after the user has left the website.

when a session is started up, a file is written to the temporary directory on the web server to contain the information for you.
*/

session_start();

if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
?>

PHP 5 OOP Polymorphism Sample/Example

<?PHP
    /*
    Polymorphism in PHP5 is a technique where the function to be called is detected based on the class object calling it at runtime. The basis of Polymorphism is Inheritance and function overridden.

    */
    class BaseClass
    {
        public function myMethod()
        {
             echo "BaseClass method called";
        }
    }
   
    class DerivedClass extends BaseClass
    {
          public function myMethod()
          {
               echo "DerivedClass method called";
          }
    }
   
    function processClass(BaseClass $c)
    {
          $c->myMethod();
    }
   
    $c = new DerivedClass();
    processClass($c);

?>

PHP 5 OOP Interface Sample/Example

<?PHP

/*
In PHP 5, interfaces may declare only methods. An interface cannot declare any variables. To extend from an Interface, keyword implements is used. PHP5 supports class extending more than one interface.
Interfaces is also an abstract class because abstract class always require an implementation. In PHP 5 class may inherit only one class, but because interfaces lack an implementation any number of class can be inherited.



PRIMARY PURPOSES OF AN INTERFACE:

    Interfaces allow you to define/create a common structure for your classes – to set a standard for objects.
    Interfaces solves the problem of single inheritance – they allow you to inject ‘qualities’ from multiple sources.
    Interfaces provide a flexible base/root structure that you don’t get with classes.
    Interfaces are great when you have multiple coders working on a project – you can set up a loose structure for programmers to follow and let them worry about the details.

WHEN SHOULD YOU MAKE A CLASS AND WHEN SHOULD YOU MAKE AN INTEFACE?

    If you have a class that is never directly instantiated in your program, this is a good candidate for an interface. In other words, if you are creating a class to only serve as the parent to other classes, it should probably be made into an interface.
    When you know what methods a class should have but you are not sure what the details will be.
    When you want to quickly map out the basic structures of your classes to serve as a template for others to follow – keeps the code-base predictable and consistent.


*/

interface employee
{
    function setdata($empname,$empage);
    function outputData();
}

class Payment implements employee
{
    function setdata($empname,$empage)
    {
            //Functionality
    }

    function outputData()
    {
        echo "Inside Payment Class";
    }
}

class Credit extends Payment implements employee
{
    function setdata($empname,$empage)
    {
            //Functionality
    }

    function outputData()
    {
        echo "Inside Credit Class";
    }
}

class Debit extends Credit implements employee
{
    function setdata($empname,$empage)
    {
            //Functionality
    }

    /*function outputData()
    {
        echo "Inside Credit Class";
    }*/
}

$a = new Debit();
$a->outputData();
?>

Problem with PHP coding and (X)HTML, CSS Design

Hi All,

If you have any problem with PHP coding and (X)HTML, CSS Design. Let me know if i can help you.

Thanks, Suraj

PHP multiple file upload in bulk

Guys!, if your in need of multiple file uploading @ 1 shot or bulk file upload in php. Let me know here.