Tuesday, May 15, 2018

OOPs in PHP

What is OOPs?
OOPs stands for Object Oriented Programming System
It is system independent of any programming language with a goal “better project management”.
OOPs is made of four core components
1.   Encapsulation
2.   Abstraction
3.   Polymorphism
4.   Inheritance
These core components also require three other components
1.   class
2.   object or instance
3.   reference
What is class?
A class is a set of specifications or blueprint about an entity describing all possible data members and the methods to be applicable on that entity.
Use class keyword to make a class.
class <entityname>
{
}
A class can have different types of components
1.   data members
2.   methods (functions)
a.   Use function keyword to define a function
3.   constructor
a.   Used to initialize data in data members
4.   setter and getter
a.   setters are used to update some data in data members
b.   getters are used to return some data in data members
Example
Create a class Customer having data members as cid, name, balance.
Create a constructor to initialize data in the data members.
Create setter methods to update the data in data members
Create getter methods to return the values in data members
Create a method deposit() to deposit some amount
Create a method withdraw() to withdraw some method
Create a sample record for a customer with data 123, “Rakesh”,5000
Deposit 3000 more
Withdraw 1500
Show the output in following format
Balance of <name> with customer id <cid> is <balance>


Note:
Use var keyword to declare the data members

What is constructor?
Special method inside a class with same name as class name and no return type
Use this keyword to refer the data members when data member name and the argument names are same
Use arrow operator -> to access the data members

What is an instance or object?
A real entity created based on class specification is called as instance.
Use new keyword to create an instance
Use arrow operator -> to access the data members and the methods

Full Code
<?php
class Customer
{
          var $cid,$name,$balance; //data members
          function Customer($cid,$name,$balance) //constructor
          {
                   $this->cid=$cid;
                   $this->name=$name;
                   $this->balance=$balance;
          }
          function setName($name) //setter
          {
                   $this->name=$name;
          }
          function getCid() //getter
          {
                   return $this->cid;
          }
          function getName()
          {
                   return $this->name;
          }
          function getBalance()
          {
                   return $this->balance;
          }
          function deposit($amount) //general method
          {
                   $this->balance=$this->balance+$amount;
          }
          function withdraw($amount)
          {
                             $this->balance=$this->balance-$amount;
          }
}
?>

<?php
$c=new Customer(123,"Rakesh Kumar",5000); //creating instance -- here $c is called as reference
$c->deposit(3000); //calling a funciton using reference
$c->withdraw(1500);

echo "Balanace of ".$c->getName()." with customer id ".$c->getCid()." is ".$c->getBalance()."<br>";
$c->setName("Rakesh Chaudhary");
echo "Balanace of ".$c->getName()." with customer id ".$c->getCid()." is ".$c->getBalance();
?>

Types of Members of a class

A class can have two types of members
1. static or class members
2. non-static or instance members

The class members get called using a class name without need of an instance or reference. It requires another operator called scope resolution operator :: to call such members
Syntax
classname::functioname()
Use static keyword with such functions

The instance members needs an instance and its reference and use arrow operator -> to call such members
$ref=new classname();
$ref->functioname();
No keyword is required with such members

Example of static member
Create a class General having a function factorial() which takes some number as argument and returns factorial of that number
<?php
 class General
 {
static function factorial($n)
{
$f=1;
for($i=1;$i<=$n;$i++)
  $f=$f*$i;
return $f;
}
 }

 echo General::factorial(5);
?>

Example of non-static member
Create a class General having a data member as num. Create a constructor to initalize data in num.
Create function factorial() to show factorial of num.
<?php
 class General
 {
var $num;
function General($num)
{
$this->num=$num;
}
function factorial()
{
$f=1;
for($i=1;$i<=$this->num;$i++)
  $f=$f*$i;
return $f;
}
 }

 $g=new General(6);
 $result=$g->factorial();
 echo $result;
?>

Sending SMS using PHP

<?php
if(isset($_REQUEST['mobile']) && isset($_REQUEST['message']))
{
$mobile=$_REQUEST['mobile'];
$message=$_REQUEST['message'];
$url="http://drbpsharmaacademy.com/sendmessage.php?mobile=$mobile&message=$message";
$ch = curl_init($url);
curl_exec($ch);
curl_close($ch);
}
?>
<form action="message.php" method="post">
Mobile : <input type="text" name="mobile" /><br />
Message : <input type="text" name="message" /><br />
<input type="submit" value="send message"/>
</form>


Monday, May 7, 2018

QR Code Google API Integration with PHP

<?php
$qr="nf";
if(isset($_POST['qr']))
{
$qr=$_POST['qr'];
}
?>
<form method='post' action='qrcode.php'>
Create QR Code for <input type="text" name="qr">
<input type='submit' value='Create QR Code'>
<?php
if($qr!="nf")
{
?>
<br>
<img src="http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=<?php echo

$qr;?>&chld=H|0">
<?php
}
?>
</form>

Saturday, May 5, 2018

Using PHP with Interactive Mode

<?php
function addition($a,$b){
  return $a + $b;
}
$handle = fopen ("php://stdin","r");
print("Enter First No : ");
$_a = fgets($handle);
printf("Enter Second No : ");
$_b = fgets($handle);
$sum = addition((int)$_a,(int)$_b);
print ("Sum is ".$sum);
fclose($handle);
?>


Run on command prompt
php test.php

Output
Enter First No : 6
Enter Second No : 7
Sum is 13

Wednesday, May 2, 2018

How to remove file extensions from display via htaccess?

Create a file with the name of .htaccess and save that file in root folder of your website with following lines to use the .html and .php pages without extension.

Extension also work if provided but now we don't need the extension.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html


Thursday, March 15, 2018

Sending email using PHP

To send email we need
1. The mail server
2. User ID
3. Password
4. From with syntax as SenderName <emailid>
5. To
6. Subject
7. body

Example
1. mail server : mail.impeccableitacademy.com
2. email id : demo@impeccableitacademy.com
3. password : Yvm7*2l8
4. From : Impeccable IT Academy<info@impeccableitacademy.com>
5. To : bpsharma@gmail.com
6. subject: Job Fair at RDEC
7. body : Dear all, you all are invited for job fair. Dr B P Sharma

PHP code: sendemail.php

<?php
 require_once "Mail.php";
 $from = "Impeccable IT Academy<info@impeccableitacademy.com>";
 $to = "bpsharma@gmail.com";
 $subject = "Job Fair at RDEC";
 $body = "Dear all, you all are invited for job fair. Dr B P Sharma";
 $host = "mail.impeccableitacademy.com";
 $username = "demo@impeccableitacademy.com";
 $password = "Yvm7*2l8";

 $headers = array ('From' => $from,'To' => $to,'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,'auth' => true,'username' => $username,'password' => $password));
 $mail = $smtp->send($to, $headers, $body);
 if (PEAR::isError($mail))
    echo("<p>".$mail->getMessage()."</p>");
 else
   echo("<p>Message successfully sent!</p>");
 ?>