PHP/Class/Constructor

Материал из Web эксперт
Версия от 07:00, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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 ();
?>



Adding a Constructor to PriceItem

 
<?php
class Item {
  private $name;
  function __construct( $name="item", $code=0 ) {
    $this->name = $name;
    $this->code = $code;
  }
  function getName () {
    return $this->name;
  }
}
class PriceItem extends Item {
  private $price;
  function __construct( $name, $code, $price ) {
    parent::__construct( $name, $code );
    $this->price = $price;
  }
  function getName() {
    return "(price) ".parent::getName ();
  }
}
$item = new PriceItem ("widget", 5442, 5.20);
print $item->getName ();
?>



Calling the constructor of the parent class

 
<?php
    class Cat {
    var $age;
    function Cat($new_age){
        $this->age = $new_age;
    }
    function Birthday(  ){
        $this->age++;
    }
    function Eat(  ){
        echo "Chomp chomp.";
    }
    function Meow(  ){
        echo "Meow.";
    }
}
class MyCat extends Cat {
    function MyCat($new_age) {
        parent::Cat($new_age);
    }
}
?>



Constructors and Destructors

 
<?php
     class SimpleClass {
          function SimpleClass($param) {
               echo "Created a new instance of SimpleClass!";
          }
     }
     $myinstance = new SimpleClass;
?>



Creating the Cat constructor

 
<?php
class Cat {
  function Cat(  ) {
  }
}
?>



Define and use constructor

<?php
   class book {
      private $title;
      private $isbn;
      private $copies;
      public function __construct($isbn) {
         $this->setIsbn($isbn);
         $this->getTitle();
         $this->getNumberCopies();
      }
      public function setIsbn($isbn) {
         $this->isbn = $isbn;
      }
      public function getTitle() {
         $this->title = "title";
         print "Title: " . $this->title . "<br />";
      }
      public function getNumberCopies() 
      {
         $this->copies = "5";
         print "Number copies available: " . $this->copies."<br />";
      }
   }
   $book = new book("1111");
?>



Define class as constructor parameter

<?php
class PersonWriter {
    function writeName( Person $p ) {
        print $p->getName()."\n";
    }
    function writeAge( Person $p ) {
        print $p->getAge()."\n";
    }
}
class Person {
    private $writer;
    function __construct( PersonWriter $writer ) {
        $this->writer = $writer;
    }
    function __call( $method, $args ) {
        if ( method_exists( $this->writer, $method ) ) {
            return $this->writer->$method( $this );
        }
    }
    function getName()  { 
        return "Joe"; 
    }
    function getAge() { 
        return 44; 
    }
}
$person= new Person( new PersonWriter() );
$person->writeName();
$person->writeAge();
?>



Define Constructor for Class

 <?php
  class employee {
      var $emp_code;
      var $name;
      var $address;
      var $department;
      var $sex;
      var $date_of_birth;
      var $salary;
      
      function employee($empname) {
         $this->emp_code="default value";
         $this->name=$empname;
         $this->address="default value";
         $this->department="default value";
         $this->sex="default value";
         $this->date_of_birth="default value";
         $this->salary="default value";
      }
    }
  $dave = new employee("Dave Osbourne");
  echo "Employee Code:",$dave->emp_code," \n";
  echo "Name:",$dave->name," \n";
  echo "Address:",$dave->address," \n";
  echo "Department:",$dave->department," \n";
  echo "Sex:",$dave->sex," \n";
  echo "Salary:",$dave->salary," \n";
  echo "Date of Birth:",$dave->date_of_birth," \n";
  ?>



Defining an object constructor

 
class user {
  public $username;
  function __construct($username, $password) { 
     if ($this->validate_user($username, $password)) {
       $this->username = $username;
     }
  }
}
$user = new user("A", "M");



Defining object constructors in PHP 4

 
class user {
  function user($username, $password) {
  }
}



Instantiate class by calling the constructor

<?php
   class Staff
   {
      private $ein;
      function __construct($ein)
      {
         if ($this->verify_ein($ein)) {
            echo "called";
         }
      }
   
      protected function verify_ein($ein)
      {
         return TRUE;
      }
   }
   $employee = new Staff("123");
?>



invoking parent constructors

 
<?php
class Staff
{
    protected $name;
    protected $title;
    function __construct()
    {
        echo "<p>Staff constructor called!</p>";
    }
}
class Manager extends Staff
{
    function __construct()
    {
        parent::__construct();
        echo "<p>Manager constructor called!</p>";
    } 
}
$employee = new Manager();
?>



Using Default Constructors

 
<?php 
class Dog { 
    function __construct($name="No-name", $breed="breed unknown", $price = 15) { 
        $this->name = $name; 
        $this->breed = $breed; 
        $this->price = $price; 
    } 
} 
$aDog = new Dog(); 
$tweety = new Dog("A", "a"); 
printf("<p>%s is a %s and costs \$%.2f.</p>\n", 
$aDog->name, $aDog->breed, $aDog->price); 
$tweety->price = 24.95; 
printf("<p>%s is a %s and costs \$%.2f.</p>\n", 
$tweety->name, $tweety->breed, $tweety->price); 
?>



Using the PHP 5 style constructor

 
<?php
class Cat {
  Function __constructor(  ){
  }
}
?>



Using Unified Constructors and Destructors

 
<?php
     class SimpleClass {
          function __construct($param) {
               echo "Created a new instance of SimpleClass!";
          }
          function __destruct() {
               echo "Destroyed this instance of SimpleClass";
          }
     }
     $myinstance = new SimpleClass("value");
     unset($myinstance);
?>