PHP5.3であれこれしてみる

PHP5.3使って書いてみた

ライブラリと実行ファイルの2つで

PHPのくせにメソッドチェインしまくれる

新機能で確認したのは「namespace」「use ** as **」「__invoke()」「匿名関数」の4点


ライブラリ:Example.php


<?php
namespace MOC\Example;
use ErrorException;

// {{{ class ConvertErrorException extends ErrorException
/**
* extends SPL ErrorException Object
* @class ConvertErrorException extends ErrorException
*
* @method __construct
* @method getErrorMessage
*/
class ConvertErrorException extends ErrorException {
public function __construct($message) {
$this->message = $message;
}
public function getErrorMessage() {
return $this->message;
}
}
// }}}

// {{{ class NumberErrorException extends ErrorException
/**
* extends SPL ErrorException Object
* @class NumberErrorException extends ErrorException
*
* @method __construct
* @method getErrorMessage
*/
class NumberErrorException extends ErrorException {
public function __construct($message) {
$this->message = $message;
}
public function getErrorMessage() {
return $this->message;
}
}
// }}}

// {{{ class TypeErrorException extends ErrorException
/**
* extends SPL ErrorException Object
* @class TypeErrorException extends ErrorException
*
* @method __construct
* @method getErrorMessage
*/
class TypeErrorException extends ErrorException {
public function __construct($message) {
$this->message = $message;
}
public function getErrorMessage() {
return $this->message;
}
}
// }}}

// {{{ class Converter extends ConvertErrorException
/**
* @class Converter extends ConvertErrorException
*
* @method __construct
* @method toString
* @method toNumber
* @method equals
* @method get
* @method __destruct
*/
class Converter extends ConvertErrorException {
protected $value;
protected $bool;

/**
*
* @return
*/
public function __construct() {}

/**
*
* @return
*/
public function toString() {
$this->value = (string)$this->value;
return new String($this->value);
}

/**
*
* @return
*/
public function toNumber() {
$this->value = (int)$this->value;
return new Integer($this->value);
}

/**
*
* @return
*/
public function equals($str) {
return $this->value === $str->get() ? true : false;
}

/**
*
* @return
*/
public function get() {
return $this->value;
}

/**
*
* @return
*/
public function __destruct() {}
}
// }}}

/**
* @class Integer extends Converter
*
* @method __construct
* @method __destruct
*
*/
class Integer extends Converter {
public function __construct($value) {
if (is_numeric($value)) {
$this->value = $value;
} else {
throw new NumberErrorException("");
}
}

/**
* Waring!!
* This method can't "+" orverride.
*
* @param Integer
* @return Integer
*/
public function __add($other) {
if (is_numeric($other)) {
return new Integer($this->value + $other);
} else {
throw new NumberErrorException("");
}
}

/**
* Waring!!
* This method can't "-" orverride.
*
* @param Integer
* @return Integer
*/
public function __sub($other) {
if (is_numeric($other)) {
return new Integer($this->value - $other);
} else {
throw new NumberErrorException("");
}
}

/**
* Waring!!
* This method can't "*" orverride.
*
* @param Integer
* @return Integer
*/
public function __mul($other) {
if (is_numeric($other)) {
return new Integer($this->value * $other);
} else {
throw new NumberErrorException("");
}
}

/**
* Waring!!
* This method can't "/" orverride.
*
* @param Integer
* @return Integer
*/
public function __div($other) {
if (is_numeric($other)) {
return new Integer($this->value / $other);
} else {
throw new NumberErrorException("");
}
}

/**
* Waring!!
* This method can't "" orverride.
*
* @param Integer
* @return Integer
*/
public function __mod($other) {
return new Integer();
}

/**
* Waring!!
* This method can't "" orverride.
*
* @param Integer
* @return Integer
*/
public function __pow($other) {
return new Integer();
}

/**
* Waring!!
* This method can't "" orverride.
*
* @param Integer
* @return Integer
*/
public function __and($other) {
return new Integer();
}

/**
* Waring!!
* This method can't "" orverride.
*
* @param Integer
* @return Integer
*/
public function __xor($other) {
return new Integer();
}

/**
* Waring!!
* This method can't "" orverride.
*
* @param Integer
* @return Integer
*/
public function __or($other) {
return new Integer();
}

/**
*
* @return void
*/
public function __destruct() {
parent::__destruct();
}
}

/**
* @class String extends Converter
*
* @method __construct
* @method count
* @method lower
* @method replace
* @method split
* @method strip
* @method upper
* @method __destruct
*
*/
class String extends Converter {
/**
*
* @return
*/
public function __construct($value) {
$this->value = $value;
}

/**
*
* @return
*/
public function count($chars = "") {
$patt = "/" . $chars . "/g";
$matches = array();
preg_match($patt, $this->value, $matches);
$cnt = count($matches) - 1;
return new Integer($cnt);
}

/**
*
* @return
*/
public function lower() {
return new String(strtolower($this->value));
}

/**
*
* @return
*/
public function replace($old, $new) {
if (preg_match("/^\/[^\/]+\/.*$/", $old)) {
return new String(preg_replace($old, $new, $this->value));
} else {
return new String(str_replace($old, $new, $this->value));
}
}

/**
*
* @return
*/
public function split($patt) {
if (preg_match("/^\/[^\/]+\/.*$/", $patt)) {
return new ArrayList(preg_split($patt, $this->value));
} else {
t
return new ArrayList(split($patt, $this->value));
}
}

/**
*
* @return
*/
public function strip($chars = "") {
return new String($chars ? trim($this->value, $chars) : trim($this->value));
}

/**
*
* @return
*/
public function upper() {
return new String(strtoupper($this->value));
}

/**
*
* @return
*/
public function __destruct() {
parent::__destruct();
}
}

/**
* @interface Base
*
* @method get
* @method set
* @method setAttribute
* @method getAttribute
*
*/
interface Base {
public function get();
public function set($value);
public function setAttribute($name, $value);
public function getAttribute($name);
}

/**
* @interface Iterator
*
*/
interface Iterator {
public function key();
public function next();
public function current();
public function valid();
}

/**
* @class ArrayIterator implements Iterator
*
* @method key
* @method next
* @method current
* @method valid
*
*/
class ArrayIterator implements Iterator {
private $value = "";
private $index = 0;
private $items = array();
/**
*
* @return
*/
public function __construct(ArrayList $arrayList) {
$this->index = 0;
$this->items = $arrayList->get();
}

/**
*
* @return
*/
public function key() {
return new Integer($this->index);
}

/**
*
* @return
*/
public function next() {
$this->index++;
}

/**
*
* @return
*/
public function current() {
return new String($this->items[$this->index]);
}

/**
*
* @return
*/
public function valid() {
if (isset($this->items[$this->index])) {
return new Boolean(true);
} else {
return new Boolean(false);
}
}

/**
*
* @return
*/
public function __destruct() {

}
}

/**
* @class ArrayList
*
* @method __construct
* @method __toString
* @method append
* @method remove
* @method reverse
* @method sort
* @method __destruct
*
*/
class ArrayList extends ArrayIterator {
private $items;

/**
*
* @return
*/
public function __construct($items=array()) {
$this->items = $items;
parent::__construct($this);
}

/**
*
* @return
*/
public function __toString() {
return $this->items;
}

/**
*
* @return
*/
public function get() {
return $this->items;
}

/**
*
* @return
*/
public function append($value) {
array_push($this->items, $value);
return new ArrayList($this->items);
}

/**
*
* @return
*/
public function remove() {}

/**
*
* @return
*/
public function reverse() {}

/**
*
* @return
*/
public function sort() {}

/**
*
* @return
*/
public function __destruct() {

}
}

/**
* @class Object
*
* @method
*
*/
class Object {
public static $instance;

/**
*
* @return
*/
public function __construct() {}

/**
*
* @return
*/
public function __invoke() {
if (!isset($this->instance)) {
$this->instance = new Object();
}
return $this->instance;
}

/**
*
* @return
*/
public function __class() {}
public function __eq() {}
public function __ne() {}
public function __init() {}
public function __new() {}
public function __str() {}
public function __destruct() {}
}

/**
* @class Type extends Object implements Base
*
* @method get
* @method set
* @method getAttribute
* @method setAttribute
*
*/
class Type extends Object implements Base {
protected $name;
protected $value;

/**
*
* @return
*/
public function get() {
return $this->value;
}

/**
*
* @return
*/
public function set($value) {
$this->value = $value;
}

/**
*
* @return
*/
public function getAttribute($name) {
return $this->$name;
}

/**
*
* @return
*/
public function setAttribute($name, $value) {
$this->$name = $value;
}
}


/**
* @class Template extends Type
*
* @method
*
*/
class Template extends Type {
public function __construct() {}
public function __destruct() {}
}


/**
* @class Boolean
*
* @method __construct
* @method ifTrue
* @method ifFalse
*
*/
class Boolean {
private $bool;

/**
*
* @return
*/
public function __construct($bool) {
$this->bool = $bool;
}

/**
*
* @return
*/
public function ifTrue() {
return $this->bool === true ? true : false;
}

/**
*
* @return
*/
public function ifFalse() {
return $this->bool === false ? true : false;
}
}

/**
* @class None
*
* @method __construct
* @method __invoke
* @method __toString
* @method getInstance
*/
final class None {
private static $instance = null;
/**
*
* @return
*/
public function __construct() {
return null;
}

/**
*
* @return
*/
public function __invoke() {
return $this->getInstance();
}

/**
*
* @return
*/
public function __toString() {
return null;
}

/**
*
* @return
*/
public function getInstance() {
if ($this->instance === null) {
$this->instance = new None();
}
return $this->instance;
}
}
?>

実行ファイル:main.php


#!/usr/local/php5.3/bin/php

require_once("Example.php");
use MOC\Example as Moc;
<?php
function main () {
$e = function(){throw new Moc\ConvertErrorException("no valid exception\n");};
$i = new Moc\Integer(1);
$s = new Moc\String("1987");
$a = new Moc\ArrayList(array('A','B',1,2,5,6));
$n = new Moc\None();
var_dump($i->__add(1000)->__sub(1)->get(), $s->toNumber()->__div(3)->get(), $s->equals($i));
echo "----\n";
var_dump($s->toString()->replace("1","1000000")->get());
echo "----\n";
var_dump($a->current()->get(), $a->next(), $a->current()->get(), $a->valid()->ifTrue() ? $n() : $e());
echo "----\n";
var_dump($i instanceof Moc\Integer);
echo "----\n";
var_dump($s instanceof Moc\String);
echo "----\n";
var_dump($a instanceof Moc\ArrayList);
echo "----\n";
var_dump($n, $n());
}

try {
main();
} catch(Moc\ConvertErrorException $e) {
print($e->getErrorMessage());
// ...
} catch (ErrorException $e) {
print($e->getErrorMessage());
// ...
} catch(exception $e) {
var_dump($e);
}
?>