PHP/Class/Interface

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

A Sample Interface

   <source lang="html4strict">

<?php

    interface printable {
         public function printme();
    }
    class Integer implements printable {
         private $value;
         public function getValue() {
              return (int)$this->value;
         }
         public function printme() {
              echo (int)$this->value;
         }
    }

?>

 </source>
   
  


Defining and Using an Interface

   <source lang="html4strict">

<?php interface Serializable {

 public function writeObject();

} class Point implements Serializable {

 public $x;
 public $y;
 public function writeObject() {
   return serialize( $this );
 }

} class Item implements Serializable {

 public function writeObject() {
   return serialize( $this );
 }

} class Serializer {

 private $sArray = array();
 function register( Serializable $obj ) {
   $this->sArray[] = $obj;
 }
 function output() {
   foreach ( $this->sArray as $obj ) {
     print $obj->writeObject()."\n";
   }
 }

} $serializer = new Serializer(); $serializer->register( new Item() ); $serializer->register( new Point() );

print "
";
$serializer->output();
print "
";

?>

 </source>
   
  


Defining an interface

   <source lang="html4strict">

interface Nameable {

   public function getName();
   public function setName($name);

} class Book implements Nameable {

   private $name;
   public function getName() {
       return $this->name;
   }
   
   public function setName($name) {
       return $this->name = $name;
   }

}

 </source>
   
  


Different class implements one interface

   <source lang="html4strict">

<?php class Person {

   public $name;
   function __construct( $name ) {
       $this->name = name;
       print "Person constructed with $name\n";
   }

} interface Command {

   function execute();

} class FtpCommand implements Command {

   function setHost( $host ) {
       print "FtpCommand::setHost(): $host\n";
   }
   function setUser( $user ) {
       print "FtpCommand::setUser(): $user\n";
   }
   function execute() {
   }

} class PersonCommand implements Command {

   function setPerson( Person $person ) {
       print "PersonCommand::setPerson(): {$person->name}\n";
   }
   
   function execute() {
   }

} ?>

      </source>
   
  


Implement an interface

   <source lang="html4strict">

<?php interface Chargeable {

   public function getPrice();

} class Employee implements Chargeable {

   protected $price;
   public function getPrice() {
       return $this->price;
   }

} $product = new Employee(); ?>

      </source>
   
  


Implementing Multiple Interfaces

   <source lang="html4strict">

<?php

       interface printable {
               public function printme();
       }
       interface Inumber {
               public function reset();
       }
       class Integer implements printable, Inumber {
               private $value;
               function __construct($value) {
                       $this->value = $value;
               }
               public function printme() {
                       echo (int)$this->value;
               }
               public function reset() {
                       $this->value = NULL;
               }
       }
       function resetNumber(Inumber $obj) {
               $obj->reset();
       }
       function printNumber(printable $obj) {
               $obj->printme();
       }
       $inst = new Integer(10);
       printNumber($inst);
       resetNumber($inst);

?>

 </source>
   
  


Using Type Hinting with Interfaces

   <source lang="html4strict">

<?php

    interface printable {
         public function printme();
    }
    abstract class Number {
         private $value;
         abstract public function value();
         public function reset() {
              $this->value = NULL;
         }
    }
    class Integer extends Number implements printable {
         private $value;
         function __construct($value) {
              $this->value = $value;
         }
         public function getValue() {
              return (int)$this->value;
         }
         public function printme() {
              echo (int)$this->value;
         }
    }
    function printNumber(printable $myObject) {
         $myObject->printme();
    }
    $inst = new Integer(10);
    printNumber($inst);

?>

 </source>