Tag Archives: PHP Scripts

How can we redeclare a class safely in PHP?

Basically PHP doesn’t allow re-declare a class directly. If you are in need to get re-declare a class in php, then I suggest you to write that class in a separate file and use require_one to call that file to the desired page. It’s as follows:

Page1.php

class abcd
{
    function max()
    {
        echo "Hello World!!! count:-".$GLOBALS['x'];
    }
}

Page2.php

$i=10; 
for($x=0;$x<$i;$x++)
{
      require_once "Page1.php";
      $myclass = new abcd();
      $myclass->max();
}

Now it will work as you desired. It worked for me.

The output will be as follows :

 Hello World!!! count:- 0
 Hello World!!! count:- 1
 Hello World!!! count:- 2
 Hello World!!! count:- 3
 Hello World!!! count:- 4
 Hello World!!! count:- 5
 Hello World!!! count:- 6
 Hello World!!! count:- 7
 Hello World!!! count:- 8
 Hello World!!! count:- 9

How to split a group of numbers to an array in PHP?

To split a number 187954 to an array as follows:

<?php
          $x=187954;
          $y=(string)$x;
          echo $y[0]."<br/>";  //1
          echo $y[1]."<br/>";  //8
          echo $y[2]."<br/>";  //7
          echo $y[3]."<br/>";  //9
          echo $y[4]."<br/>";  //5
          echo $y[5]."<br/>";  //4
?>

The Output will be as follows:

    1
    8
    7
    9
    5
    4

Advanced mail sending method in PHP

It’s as follows :

Page 1:config.php
class mymailer
    {
        function mysendmail($from,$to,$subject='sub',$body='msg',$content_type='text/html',$auth=true,$host=MAIL_HOST,$username=MAIL_USERNAME,$password=MAIL_PASSWORD)
        {
            require_once "Mail.php";   //you should install PEAR::Mail module in your server
            $headers = array ('From' => $from,
                'To' => $to,
                'Subject' => $subject,
                'Content-type'=>$content_type
            );
            $smtp = Mail::factory('smtp',
                array ('host' => $host,
                'auth' => $auth,
                'username' => $username,
                'password' => $password)
            );
            $mail = $smtp->send($to, $headers,$body);
            if (PEAR::isError($mail))
            {
                //return $mail->getMessage();
                return 'F';
            }
            else
            {
                return 'S';
            }
        }
    }

You can call this class as :

Page 2: mail_sending_page.php
require_once "config.php";
$from="frommail@company.com";
$to="tomail@example.com";
$subject="My Mail Subject";
$body="This is the body of this mail. Your content goes here.";
$mails=new mymailer;
$mails->mysendmail($from,$to,$subject,$body);

How to get array length in php?

It’s as follows :-

<?php
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);       //we get the length of array here.
?>

PHP class to generatre UUID

It can be written as follows :-

    class uuidgen
    {
        function uuid($serverID=1)
        {
            $t=explode(" ",microtime());
            return sprintf( '%04x-%08s-%08s-%04s-%04x%04x',
                $serverID,
                $this->clientIPToHex(),
                substr("00000000".dechex($t[1]),-8),   // get 8HEX of unixtime
                substr("0000".dechex(round($t[0]*65536)),-4), // get 4HEX of microtime
                mt_rand(0,0xffff), mt_rand(0,0xffff));
        }
        function uuidDecode($uuid)
        {
            $rez=Array();
            $u=explode("-",$uuid);
            if(is_array($u)&&count($u)==5)
            {
                $rez=Array(
                    'serverID'=>$u[0],
                    'ip'=>$this->clientIPFromHex($u[1]),
                    'unixtime'=>hexdec($u[2]),
                    'micro'=>(hexdec($u[3])/65536)
                );
            }
            return $rez;
        }
        function clientIPToHex($ip="")
        {
            $hex="";
            if($ip=="")
            {
                $ip=getEnv("REMOTE_ADDR");
            }
            $part=explode('.', $ip);
            for ($i=0; $i<=count($part)-1; $i++)
            {
                $hex.=substr("0".dechex($part[$i]),-2);
            }
            return $hex;
        }
        function clientIPFromHex($hex)
        {
            $ip="";
            if(strlen($hex)==8)
            {
                $ip.=hexdec(substr($hex,0,2)).".";
                $ip.=hexdec(substr($hex,2,2)).".";
                $ip.=hexdec(substr($hex,4,2)).".";
                $ip.=hexdec(substr($hex,6,2));
            }
            return $ip;
        }
    }

To get uuid call the class as follows :-


$x=$uuid->uuid();
echo $x