[ create a new paste ] login | about

Link: http://codepad.org/6LAU1QjZ    [ raw code | output | fork ]

fedecarg - PHP, pasted on Aug 17:
<?php
class Book {
  private $title;

  public function __get($property) {
    if (!property_exists($this, $property)) {
      throw new Exception('No such property: ' . $property);
    }

    $method = 'get' . ucfirst($property);
    if (!method_exists($this, $method)) {
      throw new Exception('No such method: ' . $method);
    }

    return $this->$method();
  }

  public function __set($property, $value) {
    if (!property_exists($this, $property)) {
      throw new Exception('No such property: ' . $property);
    }

    $method = 'set' . ucfirst($property);
    if (!method_exists($this, $method)) {
      throw new Exception('No such method: ' . $method);
    }

    $this->$method($value);
  }

  public function setTitle($title) {
    $this->title = $title;
  }

  public function getTitle() {
    return $this->title;
  }
}

$book = new Book;
$book->title = 'Code Complete';
print $book->title;


Output:
1
Code Complete


Create a new paste based on this one


Comments: