Wednesday, February 10, 2010

Object Oriented PHP and Basic example OOP

Object-oriented programming (OOP) is a programming paradigm that uses "objects" – data structures consisting of datafields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, modularity, polymorphism, and inheritance.
- wikipedia.org

let's go....

Define class
------------
Syntax Class classname { }


Class TheGreatestPost
{
      public $word;
      public function print()
      {
       echo $this->word;
       }
}



To print output
---------------

$post = new TheGreatestPost(); //instantiate the object
$post->word = 'hello The Greatest Post';
$post->print();


::output:: “hello The Greatest Post”.


Using    _construct()
-----------------
special method that is used in the first instantiation of an object that allows you to construct the object when it is invoked, as it is triggered whenever a new object is created.



Class TheGreatestPost
{
     public $word;
     public function __construct($word)
    {
        $this->word = $word;
    }

   public function print()
   {
       echo $this->word;
    }
}

$post = new TheGreatestPost('hello The Greatest Post');
$post->print();


::output:: “hello The Greatest Post”




Class Inheritance
-----------------
Through this, an extended class inherits methods and members of its parent class defined in the extend declaration.




class TheGreatestPost
{
    public function print()
    {
      echo $this->word;
     }
}

class TheGreatestPost2 extends TheGreatestPost
{
     protected $word = 'hello The Greatest Post';
}

$post = new TheGreatestPost2();
$post->print();



::output:: hello The Greatest Post

 
Simple code created by thegreatestpost.


Need more tutorial?? visit: http://www.killerphp.com/tutorials/object-oriented-php/ ( include video tutorial)
thanks to killerphp.com for video tutorial.

No comments: