PHP/Class/Interface — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 10:37, 26 мая 2010

A Sample Interface

 
<?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;
          }
     }
?>



Defining and Using an Interface

 
<?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 "<pre>";
$serializer->output();
print "</pre>";
?>



Defining an interface

 
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;
    }
}



Different class implements one interface

<?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() {
    }
}
?>



Implement an interface

<?php
interface Chargeable {
    public function getPrice();
}
class Employee implements Chargeable {
    protected $price;
    public function getPrice() {
        return $this->price;
    }
}
$product = new Employee();
?>



Implementing Multiple Interfaces

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



Using Type Hinting with Interfaces

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