Wednesday, 29 February 2012

What is Constructor?

Lets revisit that definition in more simple terms. A constructor is a special function – this means that a constructor is a function; but its special. But, why is it special? It’s special because it is automatically executed or called when an object of a class is created.

We need a Constructor?
It is needed as it provides an opportunity for doing necessary setup operations like initializing class variables, opening database connections or socket connections, etc. In simple terms, it is needed to setup the object before it can be used.

class Customer {
 public function __construct() {
  //code
 }
}
Let’s look at a real example:
class Customer {
 private $first_name;
 private $last_name;
 private $outstanding_amount;
 
 public function __construct() {
  $first_name = "";
  $last_name = "";
  $outstanding_amount = 0;
 }
 
 public function setData($first_name, $last_name, $outstanding_amount) {
  $this->first_name = $first_name;
  $this->last_name = $last_name;
  $this->outstanding_amount = $outstanding_amount;
 }
 
 public function printData() {
  echo "Name : " . $first_name . " " . $this->last_name . "\n";
  echo "Outstanding Amount : " . $this->outstanding_amount . "\n";
 }
 
}
 
$c1 = new Customer();
$c1->setData("Sunil","Bhatia",0);

Wednesday, 22 February 2012

Q: what is interface?

We can declare methods and constants in interface. Make sure that declared method must have only declaration part and not the implementation part. Also all methods declared in interface must be public.

Create Interface

We can create an interface using the keyword interface. Check out below code that will demonstrate the code to create an basic interface..
interface first_interface
  {
    public function first_method($name);
    public function second_method();
  }
You can see that above interface have 2 methods declared. So any class which implement this interface must declare those two methods. 
Implementing the Interface
Interface can be implemented using implements operator. All methods declared in the interface must be implemented in class which implement that interface.
It will generate Fatal Error if you do not implement the method which declared in an interface. Please check below code which shows the implementation of the interface.
interface first_interface
  {
    public function first_method($name);
    public function second_method();
  }

 class new_class implements first_interface
 {
    public function first_method($name)
    {
      // Code goes Here for method
    }

    public function second_method()
    {
      // Code goes Here  for method
    }
 }
  We can implement more than one interface to one class. In this case interface names should be separated by comma ,.
class new_class implements first_interface, second_interface
  {
     // Code for the class goes here.
  }

Q: what is inheritance ?


For example, when you extend a class, the subclass inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality
<?php

class foo
{
    public function printItem($string)
    {
        echo 'Foo: ' $string PHP_EOL;
    }
    
    public function printPHP()
    {
        echo 'PHP is great.' PHP_EOL;
    }
}

class bar extends foo
{
    public function printItem($string)
    {
        echo 'Bar: ' $string PHP_EOL;
    }
}

$foo = new foo();
$bar = new bar();
$foo->printItem('baz'); // Output: 'Foo: baz'
$foo->printPHP();       // Output: 'PHP is great' 
$bar->printItem('baz'); // Output: 'Bar: baz'
$bar->printPHP();       // Output: 'PHP is great'

?>

Q: What is oops in php with example?


Object Oriented Programming  is a paradigm which is nowadays the most popular way to develop any application and most of the modern day language is based on this paradigm.
·  Class
·  Object
·  Polymorphism
·  Dynamic Binding...etc
Simply put, an oject is a bunch of variables and functions all lumped into a single entity. The object can then be called rather than calling the variables or functions themselves. Within an object there are methods and properties. The methods are functions that manipulate data withing the object. The properties are variables that hold information about the object.
A class is the blueprint for your object. The class contains the methods and properties, or the charactaristics of the object. It defines the object. Lets just start with some examples to see how it all pieces together. We will use a vehicle as our object. All vehicles share similar charactaristics, eg: number of doors, they are painted some color, they each have a price. All vehicles do similar things also, drive, turn left, turn right, stop etc. These can be described as functions, or in OOP parlance, methods. So, the class holds the definition, and the object holds the value. You declare class in PHP by using the class keyword.
<?php
class vehicle{
/*** define public properties ***/

/*** the color of the vehicle ***/
public $color;

/*** the number of doors ***/
public $num_doors;

/*** the price of the vehicle ***/
public $price;

/*** the shape of the vehicle ***/
public $shape;

/*** the brand of vehicle ***/
public $brand;

/*** the constructor ***/
public function __construct(){
  echo 
'About this Vehicle.<br />';
}

/*** define some public methods ***/

/*** a method to show the vehicle price ***/
public function showPrice(){
  echo 
'This vehicle costs '.$this->price.'.<br />';
}

/*** a method to show the number of doors ***/
public function numDoors(){
  echo 
'This vehicle has '.$this->num_doors.' doors.<br />';
}

/*** method to drive the vehicle ***/
public function drive(){
  echo 
'VRRROOOOOOM!!!';
}

/*** end of class ***/
?>

Q: what is strtoupper?


strtoupperMake a string uppercase
<?php
$str 
"Mary Had A Little Lamb and She LOVED It So";
$str strtoupper($str);
echo 
$str// Prints MARY HAD A LITTLE LAMB AND SHE LOVED IT SO
?>