1 개요[ | ]
- trait
- 트레이트
- 클래스 기능을 확장하기 위해 사용하는 메소드들의 집합
2 예시: PHP[ | ]
- 템플릿
PHP
Copy
trait TSingleton {
private static $_instance = null;
public static function getInstance() {
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
}
class FrontController {
use TSingleton;
}
class WebSite extends SomeClass {
use TSingleton;
}
PHP
Copy
trait TBounding {
public $x, $y, $width, $height;
}
trait TMoveable {
public function moveTo($x, $y) {
// ...
}
}
trait TResizeable {
public function resize($newWidth, $newHeight) {
// ...
}
}
class Rectangle {
use TBounding, TMoveable, TResizeable;
public function fillColor($color) {
// ...
}
}