Friday, 20 December 2013

Constructor

                  Constructor is a built in function of an object.Constructors are allow us to initialize an object's properties.

Example:

<?php
class pencilbox {
 var$name;
 function __construct($name)
 {
 $this->name = $name;
}
 function set_name($name)
{
$this->name = $name;
 }

function get_name()
{
return $this->name;
 }

}  ?>

Explanation:

  • When we are instantiating a class (Creating an object),a property will be passed to the class.
  • Here $name is the property.

Wednesday, 11 December 2013

Object

           Objects are instances of a class.So the process of instantiating the class provides an object.We can create unlimited times of objects with a class.
           When we are creating an object... that is instantiating a class starts with a keyword of  'new'.

Example:

             <?php     include("myclass.php");    ?>




              <head>







                <!-- head contents -->
              </head>
              <body>
              <?php
                   $pen=new pencilbox();
                ?>
               </body>
Explanation:
  • First include the class file
  • Then call instantiating function where you need it in the program....
  • Here $pen is the object  instantiated from the class pencilbox().

Wednesday, 4 December 2013

Class

Definition:
  • Classes are user defined data types.
  • Classes are collection of objects which are in similar datatype.
  • We can create any number of objects under a single class.
  • Class has 2 main parts 
            1.Properties:
                       Properties are nothing but simple variables.
            2.Methods:
                       Methods are functions of classes,used to manipulate data/variables/properties.

Sample code:1


      class pencilbox()
        {
             var $type;

             function set_type($mytype)
                {
                   $this->type=$mytype;
                 }


        }

Explanation:

  • Here we are creating a class with the name pencilbox .
  • $type-Property
  • function set_type($mytype)-Method

Sample code :2


               class pencilbox()
        {
             var $type;

             function set_type($mytype)
                {
                   $this->type=$mytype;
                 }
            function get_type()
                {
              return   $this->type;
                 }

        }

Explanation:
  •      Setter method is used to set a property.[function set_type($mytype)]
  •      Getter method is used to get a property [function get_type()]
  •       $this : is a builtin variable which points to the current. property.[$this->property name]
     

Object oriented programing

    OOP in php is about creating a modular code like other languages(java......).OOP makes it easy to maintain and modifying the existing code.

  Concepts of oops:
  • Classes
  • Objects
  • Data Abstraction and Encapsulation
  • Inheritance
  • Polymorphism

              Before entering into the concepts  , create 2 files named index.php,myclass.php.