Tuesday, 9 August 2011

OOPS

                                    
                                                     ===================================
Class:-A class is a collection of variables and functions working  with variables .A class is a user defined data type.
Syntax:-        class  classname
{ 
  // member declaration
   Private   $var1='hello”;  
   Public   $var2 = 'a default value';          
   Protected $var3='myprotected';
   // method declaration
   Public function display ()
     {
       echo "hai php firends";
     }
 }
object:-  it is an instance of class.
creating an object:-  $instance = new classname();
we can assign an object into a variable.      $myvariable=$instance;
Calling a variable with object:-   $myobjecy->$variablename=5;

*Encapsulation:-   it is a concept of data hiding.
                                       (or)
*object can't access private data .it is called encapsulation.

*Abstraction:-  providing full information about entity is called abstraction.

*Inheritance:-  it is the concept of deriving the futures from one   class to another class

*polymorphism:-writing more than one function with the same name.
Object initialization:-
<?php
class foo
{
 function do_foo()
           {
               echo "Doing foo.";
        }
      }
                        $bar = new foo;
     $bar->do_foo();
     ?>

 Convert to object:-
<?php
$obj = (object) 'ciao';
echo $obj->scalar;  // outputs 'ciao'
?>
what is THIS:-
          1)       when instance variable and local variable names are same
               then this keyword is requred to access instance variable.
2)         this works like an object for current class.
                    use of this:-when no of variables are more these can be maintained
             easily by giving same names.




Example on THIS:-
class A
{
   function foo()
   {
       if (isset($this)) {        
           echo '$this is defined (';
           echo get_class($this);
           echo ")\n";
       } else {
           echo "\$this is not defined.\n";
       }
   }
}
          class B
{
   function bar()
   {
       A::foo();         
   }
}
$a = new A();
$a->foo();
A::foo();
$b = new B();
$b->bar();
B::bar();      
?>
         

Constructor:-
1 Constctor is a special type of method which can be invoked automatically whle 
   an object is creating.
2 Constructor syntax:-
Function __construct()
                    {       
echo “hai friends this is constructor”;
}
3 constructor overloading is not possible in php, but it is possible in other languages
   l  ike java,c#.c++.
Simple program on constructor:-
<?php
class BaseClass {
  function __construct() {
      print "In BaseClass constructor\n";
  }
}
$obj = new BaseClass();
?>
 But constructors support the inheritance.

Destructor:-
Destructor is a special type of method which can be automatically invoked while
an object is destroying.Generally destructors can be used to deallocate the
memory.

Syntax:-
Function __destruct ()
{       
echo “this is destructor”;
}
Simple program on destructor:-
<?php
class MyDestructableClass {
  function __construct() {
      print "In constructor\n";
      $this->name = "MyDestructableClass";
                             }
           function __destruct() {
      print "Destroying " . $this->name . "\n";
}
        }
$obj = new MyDestructableClass();
?>



Scope resolution operator:-  ::
          It is a token that will allows to access static, constants.
Static:-declaring the functions(methods) and variables  as static, we can access without
need to instantiation(without need to create object) of class. The static declaration must
be after the visibility declaration.Because static methods are callable without an instance
of the object created. Static properties cannot be accessed through the object using the
arrow operator ->.

How to declare a static function:-
public static function aStaticMethod()
{
‘’’’
}

How to declare a static variable:-    public static $my_static = 'foo';

how to create the class constant and how to access with scope resolution operator:-
<?php 
class MyClass {
   const CONST_VALUE = 'A constant value';
}
          echo MyClass::CONST_VALUE;
?>
<?php
class OtherClass extends MyClass
{
   public static $my_static = 'static var';
             public static function doubleColon() {
       echo parent::CONST_VALUE . "\n";
       echo self::$my_static . "\n";      }
}
OtherClass::doubleColon();
?>
Overloading:-
Php supports member overloading as well as method overloading.
Overloading is a property of assigned a new functionality ,to already existing thing .
if that is a operator ,variable,(or) function.
Example:-<?php
class Caller
{
   private $x = array(1, 2, 3);
                                public function __call($m, $a)
   {
       print "Method $m called:\n";
       var_dump($a);
       return $this->x;
   }
}
$foo = new Caller();
$a = $foo->test(1, "2", 3.4, true);
var_dump($a);
       ?>

The above example will output:
copy to clipboard
Method test called:
array(4) {
                                      [0]=> int(1)
[1]=> string(1) "2"
[2]=> float(3.4)       
[3]=> bool(true)
}
array(3) {      [0]=>   int(1)
[1]=>   int(2)
[2]=>   int(3)
} ?>


Abstract class  and  interface:-
1->When a class is not providing the full cunctionality, it need to be declared 
    as abstract class.
2->Meaning:- A method with out body is called a  abstract class.
      //in c++ this is called as pure virtual functions
3->When a class contasins atleast one abstract method ,then that class must
    be declared as abstract class
4->abstract methods must be overrided
5->when a class contains with body and without body it is called partial abstract
    class
6->if it contains only methods without bod then it is recvommended to declared as     
     interface.
Note:-interface contains only abstract methods.
7->abstract class is not instantiable.

Example of interface:-
<?php
// Declare the interface 'iTemplate'
interface iTemplate
{
   public function setVariable($name, $var);
   public function getHtml($template);
}
         
// Implement the interface
// This will work

class Template implements iTemplate
{
   private $vars = array();
             public function setVariable($name, $var)
   {
       $this->vars[$name] = $var;
   }
             public function getHtml($template)
   {
       foreach($this->vars as $name => $value) {
           $template = str_replace('{' . $name . '}', $value, $template);
       }
                 return $template;
   }
}

Object cloning:-
$copy_of_object = clone $object;
When an object is clone PHP 5 will perform a shallow copy of all of the object's
properties.Creating a copy of an object ,it is not need to get  all the properties 
of  object. Copy constructor supports ………..
Object comparision:- (==)//pending
Inheritance:-
Inheritance is a concept of deriving the feature from one class into another class.
Inheritance leads code reusability
3)inheritance saves memory,saves time

Types of inheritances:-
1->single inheritance
2->multilevel inheritance
3->hirarichal inheritance
4->multiple inheritance
5->hybrid inheritance(it is a collection of all the above 4 types)

NOTE:       But php supports only single inheritance.

Give me some examples of magic methods:-__construct,__destruct,__call,__get,__set,__iset,__unset,__sleept,__wakeup
these are the examples of magic methods.

What is magic methods?
Magic methods have predefined functionality ,if you use,with your own functionality,
it will work only magic functionality.

What is __sleep,__wakeup(serialize( )    ):-
If u use more then one __sleep function in our program ,then that time we need a
function that is used to execute the function concurrently thay method is called serialize().Generally this function is useful in multi threading concepts.

How can u connect to database with classes:-
<?php
    class Connection {
  protected $link;
  private $server, $username, $password, $db;
  public function __construct($server, $username, $password, $db)
                                      {
       $this->server = $server;
       $this->username = $username;
       $this->password = $password;        
       $this->db = $db;
       $this->connect();
     }
                                       private function connect()
   {
$this->link = mysql_connect($this->server, $this->username, $this->password);
mysql_select_db($this->db, $this->link);
 }
                                       public function __sleep()
   {
       mysql_close($this->link);
   }
                                     public function __wakeup()
   {
       $this->connect();
   }
}
?>

Extends:- (inheritance)
<?php
$assigned   =  $instance;
$reference  =& $instance;
$instance->var = '$assigned will have this value';
$instance = null; // $instance and $reference become null
var_dump($instance);
var_dump($reference);
var_dump($assigned);
?>

<?php
class ExtendClass extends SimpleClass
{
   // Redefine the parent method
   function displayVar()
   {
       echo "Extending class\n";
       parent::displayVar();
   }
}
$extended = new ExtendClass();
$extended->displayVar();
?>

Output:-
Extending class
a default value
@reflection



                         



No comments:

Post a Comment

Visual comparison of the two methods, creating a simple table.

Option 1, using PHP: // PHP $html = '<table>' ;     foreach ( $data as $row ) {     $html .= '<tr>...