# Proxy in PHP (ok)

C:\xampp8\htdocs\lva\index.php

```php
<?php
include("Book.php");
include("BookList.php");
include("ProxyBookList.php");
writeln('BEGIN TESTING PROXY PATTERN');
writeln('');
$proxyBookList = new ProxyBookList();
$inBook = new Book('PHP for Cats', 'Larry Truett');
$proxyBookList->addBook($inBook);
writeln('test 1 - show the book count after a book is added');
writeln($proxyBookList->getBookCount());
writeln('');
writeln('test 2 - show the book');
$outBook = $proxyBookList->getBook(1);
writeln($outBook->getAuthorAndTitle());
writeln('');
$proxyBookList->removeBook($outBook);
writeln('test 3 - show the book count after a book is removed');
writeln($proxyBookList->getBookCount());
writeln('');
writeln('END TESTING PROXY PATTERN');
function writeln($line_in)
{
  echo $line_in . "<br/>";
}
```

C:\xampp8\htdocs\lva\Book.php

```php
<?php
class Book {
  private $author;
  private $title;
  function __construct($title_in, $author_in) {
    $this->author = $author_in;
    $this->title  = $title_in;
  }
  function getAuthor() {
    return $this->author;
  }
  function getTitle() {
    return $this->title;
  }
  function getAuthorAndTitle() {
    return $this->getTitle() . ' by ' . $this->getAuthor();
  }
}
```

C:\xampp8\htdocs\lva\BookList.php

```php
<?php
class BookList {
  private $books     = array();
  private $bookCount = 0;
  public function __construct() {}
  public function getBookCount() {
    return $this->bookCount;
  }
  private function setBookCount($newCount) {
    $this->bookCount = $newCount;
  }
  public function getBook($bookNumberToGet) {
    if ((is_numeric($bookNumberToGet)) && ($bookNumberToGet <= $this->getBookCount())) {
      return $this->books[$bookNumberToGet];
    } else {
      return NULL;
    }
  }
  public function addBook(Book $book_in) {
    $this->setBookCount($this->getBookCount() + 1);
    $this->books[$this->getBookCount()] = $book_in;
    return $this->getBookCount();
  }
  public function removeBook(Book $book_in) {
    $counter = 0;
    while (++$counter <= $this->getBookCount()) {
      if ($book_in->getAuthorAndTitle() == $this->books[$counter]->getAuthorAndTitle()) {
        for ($x = $counter; $x < $this->getBookCount(); $x++) {
          $this->books[$x] = $this->books[$x + 1];
        }
        $this->setBookCount($this->getBookCount() - 1);
      }
    }
    return $this->getBookCount();
  }
}
```

C:\xampp8\htdocs\lva\ProxyBookList.php

```php
<?php
class ProxyBookList {
  private $bookList = NULL;
  //bookList is not instantiated at construct time
  function __construct() {}
  function getBookCount() {
    if (NULL == $this->bookList) {
      $this->makeBookList();
    }
    return $this->bookList->getBookCount();
  }
  function getBook($bookNum) {
    if (NULL == $this->bookList) {
      $this->makeBookList();
    }
    return $this->bookList->getBook($bookNum);
  }
  function addBook($book) {
    if (NULL == $this->bookList) {
      $this->makeBookList();
    }
    return $this->bookList->addBook($book);
  }
  function removeBook($book) {
    if (NULL == $this->bookList) {
      $this->makeBookList();
    }
    return $this->bookList->removeBook($book);
  }
  //Create
  function makeBookList() {
    $this->bookList = new bookList();
  }
}
```

Trong mẫu proxy, một lớp đại diện cho và xử lý tất cả quyền truy cập vào lớp khác.

Điều này có thể là do đối tượng thực ở một vị trí khác (máy chủ, nền tảng, v.v.), đối tượng thực sử dụng nhiều cpu hoặc bộ nhớ để tạo và chỉ được tạo nếu cần thiết hoặc để kiểm soát quyền truy cập vào đối tượng thực. Một proxy cũng có thể được sử dụng để thêm chức năng truy cập bổ sung, chẳng hạn như ghi lại số lần chủ thể thực sự được gọi.

Trong ví dụ này, ProxyBookList được tạo thay cho BookList sử dụng nhiều tài nguyên hơn. ProxyBookList sẽ chỉ khởi tạo BookList khi lần đầu tiên một phương thức trong BookList được gọi.

In the proxy pattern one class stands in for and handles all access to another class.

This can be because the real subject is in a different location (server, platform, etc), the real subject is cpu or memory intensive to create and is only created if necessary, or to control access to the real subject. A proxy can also be used to add additional access functionality, such as recording the number of times the real subject is actually called.

In this example, the `ProxyBookList` is created in place of the more resource intensive `BookList`. `ProxyBookList` will only instantiate `BookList` the first time a method in `BookList` is called.

```
<?php

class ProxyBookList {
    private $bookList = NULL; 
    //bookList is not instantiated at construct time
    function __construct() {
    }
    function getBookCount() {
        if (NULL == $this->bookList) {
            $this->makeBookList(); 
        }
        return $this->bookList->getBookCount();
    }
    function addBook($book) {
        if (NULL == $this->bookList) {
            $this->makeBookList(); 
        }
        return $this->bookList->addBook($book);
    }  
    function getBook($bookNum) {
        if (NULL == $this->bookList) {
            $this->makeBookList();
        } 
        return $this->bookList->getBook($bookNum);
    }
    function removeBook($book) {
        if (NULL == $this->bookList) {
            $this->makeBookList();
        } 
        return $this->bookList->removeBook($book);
    }
    //Create 
    function makeBookList() {
        $this->bookList = new bookList();
    }
}

class BookList {
    private $books = array();
    private $bookCount = 0;
    public function __construct() {
    }
    public function getBookCount() {
        return $this->bookCount;
    }
    private function setBookCount($newCount) {
        $this->bookCount = $newCount;
    }
    public function getBook($bookNumberToGet) {
        if ( (is_numeric($bookNumberToGet)) && ($bookNumberToGet <= $this->getBookCount())) {
            return $this->books[$bookNumberToGet];
        } else {
           return NULL;
        }
    }
    public function addBook(Book $book_in) {
        $this->setBookCount($this->getBookCount() + 1);
        $this->books[$this->getBookCount()] = $book_in;
        return $this->getBookCount();
    }
    public function removeBook(Book $book_in) {
        $counter = 0;
        while (++$counter <= $this->getBookCount()) {
          if ($book_in->getAuthorAndTitle() == $this->books[$counter]->getAuthorAndTitle()) {
            for ($x = $counter; $x < $this->getBookCount(); $x++) {
              $this->books[$x] = $this->books[$x + 1];
          }
          $this->setBookCount($this->getBookCount() - 1);
        }
      }
      return $this->getBookCount();
    }
}

class Book {
    private $author;
    private $title;
    function __construct($title_in, $author_in) {
      $this->author = $author_in;
      $this->title  = $title_in;
    }
    function getAuthor() {
        return $this->author;
    }
    function getTitle() {
        return $this->title;
    }
    function getAuthorAndTitle() {
      return $this->getTitle().' by '.$this->getAuthor();
    }
}

  writeln( 'BEGIN TESTING PROXY PATTERN';
  writeln('');
 
  $proxyBookList = new ProxyBookList();
  $inBook = new Book('PHP for Cats','Larry Truett');
  $proxyBookList->addBook($inBook);
 
  writeln('test 1 - show the book count after a book is added');
  writeln($proxyBookList->getBookCount());
  writeln('');
 
  writeln('test 2 - show the book');
  $outBook = $proxyBookList->getBook(1);
  writeln($outBook->getAuthorAndTitle()); 
  writeln('');
 
  $proxyBookList->removeBook($outBook);
 
  writeln('test 3 - show the book count after a book is removed');
  writeln($proxyBookList->getBookCount());
  writeln('');

  writeln('END TESTING PROXY PATTERN');

  function writeln($line_in) {
    echo $line_in."<br/>";
  }

?>
```

**Output**

```
BEGIN TESTING PROXY PATTERN

test 1 - show the book count after a book is added
1

test 2 - show the book
PHP for Cats by Larry Truett

test 3 - show the book count after a book is removed
0

END TESTING PROXY PATTERN
```

<br>
