Skip to content

Instantly share code, notes, and snippets.

@polidog
Last active December 15, 2015 22:49
Show Gist options
  • Save polidog/5335966 to your computer and use it in GitHub Desktop.
Save polidog/5335966 to your computer and use it in GitHub Desktop.
<?php
abstract class Lemon {
protected $__isNigata2Properties;
protected $__isFinalProperties;
public function __construct() {
$this->__isNigata2Properties = array();
$this->__isFinalProperties = array();
$refectionClass = new ReflectionClass($this);
foreach ($refectionClass->getProperties() as $property) {
if ($property->getDocComment() === false) {
continue;
}
if ($property->isPublic()) {
continue;
}
foreach (explode("\n",$property->getDocComment()) as $value) {
$value = str_replace(array("\t", "*", "\n", " "), "", $value);
if ($this->__isNiigataAccess($value)) {
$this->__isNigata2Properties[$property->getName()] = true;
$this->__isFinalProperties[$property->getName()] = $this->__isFinalAccess($value);
break;
}
}
}
}
/**
* GETマジックメソッド
* @param string $name
* @return string
*/
public function __get($name) {
if (isset($this->__isNigata2Properties[$name])) {
$reflection = new ReflectionProperty($this,$name);
$reflection->setAccessible(true);
if ($this->__isFinalProperties[$name]) {
$refectionClass = new ReflectionClass($this);
$defaultProperties = $refectionClass->getDefaultProperties();
return $defaultProperties[$name];
} else {
return $reflection->getValue($this);
}
} else {
return parent::__get($name);
}
}
/**
* 檸檬かどうかのチェックを行う
* @param string $value
* @return boolean
*/
protected function __isNiigataAccess($value) {
return (strpos($value,"@access") !== false && strpos($value,"lemon"));
}
protected function __isFinalAccess($value) {
return (strpos($value,"@access") !== false && strpos($value,"final"));
}
}
// 以下テスト
// 変数のプロパティに対して「@access」のあとに「lemon」、「檸檬」、「れもん」とか
// つければプライベートな変数にアクセスできる
class Hoge extends Lemon {
/**
* @access lemon
*/
private $hoge = "れもん";
/**
* @access final lemon
*/
private $hoge2 = "イエロー";
public function change() {
$this->hoge = "レモン汁";
}
public function change2() {
$this->hoge2 = "ぶるー";
}
public function check() {
var_dump($this->hoge2);
}
}
$hoge = new Hoge();
var_dump($hoge->hoge);
$hoge->change();
var_dump($hoge->hoge);
var_dump("----");
var_dump($hoge->hoge2);
$hoge->change2();
var_dump($hoge->hoge2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment