Monday, February 17, 2014

PHP OOPS Part - 1 - Simple class


PHP one of the Fast growing web programming language. It contains object oriented feature for robust and complex projects design. let us create a simple class in php.
class Product
{
  public $product_id;
  public $product_name;

  public function getProduct()
  {
     echo "Product Name is ".$this->product_name.
  }
  
}

now we need to instantiate the class already created. steps are,
$productobject = new Product;

$productobject->product_name = "Colgate";

$productobject->getProduct();

The output is Product Name is Colgate
Explanation
* class keyword for create class * Product is the name of class
* $product_id and $product_name are class members (public)
* getProduct() is a public access method.
* $productobject = new Product;
new operator is used to instantiate the class
* $productobject->product_name = "Colgate";
the above line assign colgate as product name.
* $productobject->getProduct();
The above line call method getProduct from product class.

No comments:

Post a Comment