A Class with a Constructor : Constructor « Class « PHP






A Class with a Constructor

 
<?php
 class Item {
   var $name;

   function Item( $name="item") {
     $this->name = $name;
   }

   function setName( $n) {
     $this->name = $n;
   }

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

 $item = new Item("5");
 print $item->getName ();
?>
  
  








Related examples in the same category

1.Instantiate class by calling the constructor
2.Define and use constructor
3.Define class as constructor parameter
4.Define Constructor for Class
5.Adding a Constructor to PriceItem
6.Calling the constructor of the parent class
7.Constructors and Destructors
8.Defining an object constructor
9.Creating the Cat constructor
10.Defining object constructors in PHP 4
11.Using the PHP 5 style constructor
12.invoking parent constructors
13.Using Unified Constructors and Destructors
14.Using Default Constructors