phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


OOP: Class (Part 1)

Tag: OOP   Category: PHP Basic
post: 17 Nov 2007 read: 980


Now, in the computer programming world, object-oriented programming (OOP) has rapidly taken hold as the programming methodology of choice for the enterprise. The basic concept is encapsulation - the grouping of data and code elements that shar common traits inside a container known as class.

Getting Started

A class contains the definition of data elements (or properties) and functions (or methods) that share some commont trait and can be encapsulated in a single structure. A class is declared using the class structure:


  <?
  // class keyword is followed by name of class: vehicle
  class vehicle
  {
    // Data property
   	var $type;
   	
    // a method, if same name as the class, 
//it as the class constructor. Automatically called 
//whenever class is instantiated
    // have paramater $type
   	function vehicle( $type )
   	{
     		$this->type = $type;
   	}
 
  }
 
  ?>
Instantiating a Class

  <?
  class vehicle
  {
   	var $type;
   	
   	function vehicle( $type )
   	{
     		$this->type = $type;
   	}
 
  }
 
  $obj = new vehicle( 'car' );
  echo $obj->type;
 
  ?>

The new operator causes a new instance of the vehicle class to be created and assigned to $obj. And method vehicle (as constructor) will be called automatically. We can pass parameters to its directly.



Series this article:
OOP: Class (Part 1)
OOP: Classes as Namespace (Part 2)
OOP: Implementing Inheritance ( Part 3 )

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


619
posting