This demo. Main points are about the same php class uses.
public, private key,
namespace, use the command space,
require import,
interface multiplexing,
abstract abstract class,
trait code reuse
static static variables and static methods
extends inheritance
implements realization
__construct initialization
=====================
Unique.php
? < PHP namespace Bookstore \ Utils; trait of Unique { // static static properties, similar to other languages class variable Private static $ lastId = 0 ; // protected property protection, allowing only inherited classes to access protected $ the above mentioned id ; public function setId ( int $ id ) { // built-in function to determine whether the id is empty, compared with $ id == null to force a good grid point iF ( empty ( $ id )) { // Note $ this- distinction between syntax and self :: $ $ this -> ID :: = ++ Self $ lastId ; } the else { $ the this -> = ID$id; if ($id > self::$lastId) { self::$lastId = $id; } } } public function getId():int { return $this->id; } //静态方法 public static function getLastId():int { return self::$lastId; } } ?>
Person.php
? < PHP namespace Bookstore \ Domain; use Bookstore \ Utils \ of Unique; // namespace can directly use, but if this is not in the standard namespace convention locale and is not automatically load, then, need to manually locate require it. Require_once . __DIR__ '/Unique.php' ; class the Person { // usage trait is, use in the class use of Unique; // protected property protection, allowing only inherited classes to access protected $ FirstName ; protected $ Surname ; // private private property, not allow external class directly modify Private $ Email ; // constructor, a good place to initialize class public function __construct ( $ the above mentioned id , string $firstname, string $surname, string $email ) { $this->firstname = $firstname; $this->surname = $surname; $this->email = $email; //复用trait内的方法代码 $this->setId($id); } public function getFirstname():string { return $this->firstname; } public function getSurname():String { return $ the this -> Surname; } public function the getEmail (): String { return $ the this -> In Email; } // class attributes change unit public method, to package information; class through internal -> modification. public function setEmail ( String $ In Email ) { $ the this -> In Email = $ In Email ; } } ?>
Payer.php
<?php //命名空间 namespace Bookstore\Domain; interface Payer { public function pay(float $amount); public function isExtentOfTaxes(): bool; } ?>
Customer.php
<?php //命名空间 namespace Bookstore\Domain; //use Bookstore\Domain\Payer; require_once __DIR__ . '/Payer.php'; interface Customer { public function getMonthlyFee(): float; public function getAmountToBorrow(): int; public function getType(): string; } ?>
Basic.php
<?php namespace Bookstore\Domain; /* use Bookstore\Domain\Person; use Bookstore\Domain\Customer; use Bookstore\Domain\Payer; */ require_once __DIR__ . '/Person.php'; require_once __DIR__ . '/Customer.php'; require_once __DIR__ . '/Payer.php'; class Basic extends Person implements Customer, Payer { public function getMonthlyFee():float { return 5.0; } public function getAmountToBorrow():int { return 3; } public function getType(): string { return 'Basic'; } public function pay(float $amount) { echo "Paying $amount."; } public function isExtentOfTaxes(): bool { return false; } } ?>
Premium.php
<?php namespace Bookstore\Domain; /* use Bookstore\Domain\Person; use Bookstore\Domain\Customer; use Bookstore\Domain\Payer; */ require_once __DIR__ . '/Person.php'; require_once __DIR__ . '/Customer.php'; require_once __DIR__ . '/Payer.php'; class Premium extends Person implements Customer, Payer { public function getMonthlyFee():float { return 10.0; } public function getAmountToBorrow():int { return 10; } public function getType(): string { return 'Premium'; } public function pay(float $amount) { echo "Paying $amount."; } public function isExtentOfTaxes(): bool { return true; } } ?>
Book.php
<?php namespace Bookstor\Domain; class Book { public function __construct ( int $isbn, string $title, string $author, int $available = 0 ) { $this->isbn = $isbn; $this->title = $title; $this->author = $author; $this->available = $available; } public function getIsbn():int { return $this->isbn; } public function getTitle():string { return $this->title; } public function getAuthor():string { return $this->author; } public function isAvailable():bool { return $this->available; } public function getCopy():bool { if ($this->available < 1) { return false; } else { $this->available--; return true; } } public function addCopy() { $this->available++; } public function __toString() { $result = '<i>' . $this->title . '</i> - ' . $this->author; if (!$this->available) { $result .= ' <b>Not available</b>'; } else { $result .= " <b>{$this->available}</b>"; } return $result . '<br/>'; } } ?>
test.php
? < Php // using namespace, easy to manage large-scale applications in php and tissue type. Use Bookstor \ Domain \ Book; use Bookstore \ Domain \ the Customer; use Bookstore \ Domain \ the Person; use Bookstore \ Domain \ Basic; use Bookstore \ Domain \ Premium; use Bookstore \ Utils \ of Unique; // namespace can directly use, but if this is not in the standard namespace convention locale and is not automatically load, then, need to manually locate require it. require_once __DIR__ '. /Unique.php ' ; require_once __DIR__.' /Book.php ' ; require_once __DIR__.' /Customer.php ' ; require_once __DIR__.' /Person.php ' ; require_once __DIR__ . '/Basic.php'; require_once __DIR__ . '/Premium.php'; $book1 = new Book("1984", "George Orwell", 9785267006323, 12); $book2 = new Book("1984", "George Orwell", 9785267006323); $customer1 = new Basic(5, 'John', 'Doe', '[email protected]'); //$customer2 = new Customer(null, 'Mary', 'Poppins', '[email protected]'); $customer3 = new Premium(7, 'James', 'Bond', '[email protected]'); if ($book1->getCopy()) { echo'. Sale Copy. 1 a' ; } the else { echo 'CAN Not Sale a.' ; } // data type conversion, Pax almost world language. $ String1 = ( String ) $ Book1 ; $ string2 = ( String ) $ Book2 ; echo $ string1 ; echo $ string2 ; // static class method call, you can directly use the class name, instance name can also be used but are used :: symbol.. echo the Person :: getLastId (); echo 'a' ; echo $ Customer1 :: getLastId (); function checkIfValid (the Customer$customer, array $books):bool { return $customer->getAmountToBorrow() >= count($books); } echo '<br/>'; var_dump(checkIfValid($customer1, [$book1])); echo '<br/>'; var_dump(checkIfValid($customer3, [$book1])); echo '<br/>'; $basic = new Basic(1, "name", "surname", "email"); $premium = new Premium(2, "name", "surname", "email"); var_dump($basic->getId()); echo '<br/>'; var_dump($premium->getId()); echo '<br/>'; var_dump(Person::getLastId()); echo '<br/>'; var_dump(Unique::getLastId()); echo '<br/>'; var_dump(Basic::getLastId()); echo '<br/>'; var_dump(Premium::getLastId()); echo '<br/>'; //判断父类及继承关系 var_dump($basic instanceof Basic); echo '<br/>'; var_dump($premium instanceof Basic); echo '<br/>'; var_dump($basic instanceof Customer); echo '<br/>'; var_dump($premium instanceof Payer); echo '<br/>'; var_dump($basic instanceof Payer); echo '<br/>'; function processPayment($payer, float $amount) { if ($payer->isExtentOfTaxes()) { echo "What a lucky one..."; } else { $amount *= 1.16; } $payer->pay($amount); } //多态实现 processPayment($basic, 2000); echo '<br/>'; processPayment($premium, 2000); echo '<br/>'; ?>
Output:
Sale 1 copy. George Orwell - 9785267006323 11 George Orwell - 9785267006323 Not available 7 7 bool(true) bool(true) int(1) int(2) int(7) int(0) int(7) int(7) bool(true) bool(false) bool(true) bool(false) bool(false) Paying 2320. What a lucky one...Paying 2000.