Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Friday, January 11, 2013

What Interfaces Do?!

you see this same concept in OOP as well.An interface enables you to specify that an object s capable of performing a certain function,but it does not necessarily tell you how the object does so.An interface is a contract between unrelated objects to perform a common function.An object that implements this interface is guaranteeing to its users that it is capable of performing all the functions defined by the interface specification .Bicycle and footballs are totally different things; however ,object representing those items in a sporting goods store inventory system must be capable of interacting with that system.

By declaring an interface and then implementing it in your objects,you can hand completely different classes to common functions.The following example shows the rather prosaic door-and-jar analogy.

Interfaces

Sometimes you have a group of classes that are not necessarily related through an inheritance-type relationship.You may have totally different class that just happen to share some behaviors in common.For example,both a jar and a door can be opened and closed ,however,they are in no other way related .No matter the kind of jar or the kind of door,they both can carry out these activities ,but there is no other common thread between them.

Preserving the Functionality of the Parent [Final]

The following example makes use of both parent:: and self:: for accessing the Child and Ancestor classes:

saved as testInheritance.php
class Ancestor {

const NAME = "Ancestor";

function __construct()

{

print "In " . self::NAME . " constructor\n";

}

}

class Child extends Ancestor {

const NAME = "Child";

function __construct()

{

parent::__construct();

print "In " . self::NAME . " constructor\n";

}

}

$obj = new Child();


outputs:
In Ancestor constructor In Child constructor

Thursday, January 10, 2013

Preserving the Functionality of the Parent [Part 2]

 PHP supports two reserved class names that make it easier when writing OO applications. self:: refers to the current class and it is usually used to access static members, methods, and constants. parent:: refers to the parent class and it is most often used when wanting to call the parent constructor or methods. It may also be used to access members and constants. You should use parent:: as opposed to the parent’s class name because it makes it easier to change your class hierarchy because you are not hard-coding the parent’s class name.

Preserving the Functionality of The Parent [Part 1]

Sometimes you want to preserve the functionality provided by the parent .You don't need to completely override the function;you just need to add something to it . You could copy all the code from the parent method into the subclass's method ,but as you have already seen,OOP offers you better ways of doing this than just copying lines of code.

To call the functionality provided by the parent,use the syntax parent::[function name] . when you just want to add additional behavior to a method,first you call parent::[function name]  and then add your additional code. when extending a function in this way,always call the method on the parent before doing anything else.Doing so ensures that any changes to the operation of the parent won't break your code.

    because the parent class may be expecting the object to be in a certain state,or may alter the state of object ,overwrite property values,or manipulate the object 's internal data,always invoke the parent method before adding your own code when extending an inherited method.


Overriding Methods

Just because a child class inherits from a parent doesn't mean that the child class necessarily needs to use the parent class 's implementation of a function.For example, if you were designing an application that needed to calculate the area of different geometric shapes, you might have classes called Rectangle and Triangle .Both of these shapes are polygons,and as such these classes will inherit from a parent class called Polygon.

In this example , we will create two classes-Rectangle and Square .A square is a special kind of rectangle.Anything you can do with a rectangle you can do with a square ;however,because a rectangle has two different side lengths and a square  has only one, you need to do some things differently.

saved as class.Rectangle.php

<?php
   class Rectangle{
       public $height;
       public $width;
      
       public function _construct($width,$height){
           $this->width=$width;
           $this->height=$height;
       }
       public function getArea(){
           return $this->height*$this->width;
       }
   }
?>





saved as class.Square.php
<?php
  require_once('class.Rectangle.php');
  class Square extends Rectangle{
      public function _construct($size){
          $this->height=$size;
          $this->width=$size;
      }
      public function getArea(){
          return pow($this->height,2);
      }
  }
?>

Inheritance

if you were creating an application to handle inventory at car dealership,you would probably need classes like Sedan,PickUpTruck ,and MinVan that would correspond to the same types of automobiles in the dealer's inventory.Your application would need not only to show how many of these items you have in stock but also to report on the characteristics of these vehicles so that the salespeople could give the information to customers.

 In This example,we will create another two files.

saved as class.inheritance.php
<?php
  class father{
    private $_car="benz";
    private $_properties="a house";
   
   
    public function getFatherDetail(){
        $total=" father : {$this->_car},{$this->_properties}";
        return $total;
    }

   
 }
   class child extends father{
       private $_toy="PS3";
       private $_friend="Billy";

       public function getChildDetail(){
           $total=" ||| Child : {$this->_toy},{$this->_friend}";
           return $total;
       }
   }
?>



saved as testdemo.php
<?php
    require_once("class.inheritance.php");
     $objFather=new father();
     echo $objFather->getFatherDetail();
     //echo $objFather->getChildDetail();
     /*if u uncomment "echo $objFather->getChildDetail(); ", u will receive a
     error message.  it is because Father couldnt inheritance anything from child,*/

     $objChild=new child();
     echo $objChild->getChildDetail();
     echo $objChild->getFatherDetail();
?>



output:

father : benz,a house ||| Child : PS3,Billy father : benz,a house

Adding a Property

Adding a property to your class is just as easy as adding a method .You simply declare a variable inside the class to hold the value of the property.

saved as class.demo.php
 <?php
        class Demo{

          public $name; // public means everyone can access this property
        function sayHello($name){
            print "Hello  $this->name!"; /*the variable "name" doesn't need a $ while pointing itself with "$this " */
         }
   }
?>


saved as testdemo.php
 <?php
        require_once('class.demo.php');
        $objDemo=new Demo(); // objDemo pointed to a object  called Demo()
        $objDemo->name='Billy'; // assign a name to variable $name


        $objAnotherDemo = new Demo();
         $objAnotherDemo->name="Ed";

        $objDemo->sayHello();// access to the function called sayHello()
        $objAnotherDemo->sayHello();
?>

Adding a Method

The Demo class isn't particularly useful if it isn't able to do anything,so let's look at how you can create a method.Remember,a method of a class is basically just a function.By coding a function inside the braces of your class,you are adding a method to that class . Here 's an example:

saved as class.demo.php
 <?php
        class Demo{
        function sayHello($name){
            print "Hello $name!";
         }
   }
?>


saved as testdemo.php
 <?php
        require_once('class.demo.php');
        $objDemo=new Demo();
        $objDemo->sayHello('Visiter');
?>

The object is now capable of printing a friendly greeting.The -> operator is used to access all methods and properties of your objects.


Wednesday, January 9, 2013

Creating a Class

In this articles,we will know how to instantiate an object .


Save in a file called class.Demo.php
<?php
  class Demo{
 }
?>


Save in a file called testDemo.php
<?php
     require_once("class.Demo.php");
     /* in this case,we use "require_once",it is because this file is a libraries.we 
      should use"Include_once" for displaying Templates.
      */
    $objDemo=new Demo();
     //we instantiate an object "objDemo" which will point to a class "Demo"   
?>





Object

You can think of a class as blueprint constructing an object.In much the same  way that many houses can be built from the same blueprint,you can build multiple instance of an object from its class.But the blueprint doesn't specify details such as the color of the walls or type of flooring.

Class

In the real world,object have characteristics and behaviors .A car has a color,weight,manufacturer and gas tank of a certain volume.Those are its characteristics.A car can accelerate ,stop,signal for a turn,and sound the horn.


In OO technology, the characteristics of a class are known as its properties.Properties have a name and a value .Some allow their value to be changed;other do not.For example,in the Car class ,you would probably have such properties as color and weight .


Tuesday, January 8, 2013

Understanding OOP Concepts

Class : the "blueprints" for an object and the actual code that defines the properties and methods

Object : running instances of a class that contain all the internal data and state information needed for your application to  function.

inheritance : the ability to define a class of one kind as being sub-type of a different kind of class (just like:

 child class->You
 parent class->Your father.

you can have your father's all properties  e.g  house,money ,but you also have your own properties e.g your friend,your PS3 .

In inheritance, you will have your father's house,your father's money,your friend and your PS3 ,But your father can't have your friend and your PS3
)


polymorphism : allow a class to be defined as being a member of more than one category of classes(just like :

a car is "a thing with an engine " and "a thing with wheels"

)

Interfaces : defines a contract specifying that an object is capable of implementing a method without actually defining how it is to be done.

encapsulation : the ability of an object to protect access to its internal data.

Billy: just have a look,no need  to remember it right now.Because  we will see how it works soon.

OOP advantages

1,You can translate individual business requirements into individual modules of codes

2,You can reuse those codes of modules,You don't need to write the similar function
again,again and again.

3,Save time in debugging .if you discover a bug in application ,you can find it by testing
the output in your classes.

4.Easier to pick up a codes in application again after a long time.

5,Easier to co-operate with your project members due to the characteristic of modules.(
just like you are baking a chocolate cake with Billy,you bake a plain cake, Billy melt
some chocolate . All you need to do with Billy is just to assembly the plain cake with
chocolate . You don't need to worry about how Billy melts chocolate.
)

What is Object-Oriented Programming ?

Object-Orient Programming(OOP) is a different way of programming an  applications."Object" enable you to more closely model in code the real-world tasks,processes and idea that your application is designed to handle.


For example,when house is being constructed , the plumbers(class A) deal with the pipes,and the electricians(class B) deal with the wires.the plumbers don't need to know how wires were made,electricians don't need to know how pipes were made. they only concern on themselves only with their own activities. 
the plumbers----class A
the electricians-----class B
The house-----application(your program)