346 | | public function toArray() { |
347 | | $arrResults = array(); |
348 | | $objReflect = new ReflectionClass($this); |
349 | | $arrProperties = $objReflect->getProperties(); |
350 | | |
351 | | foreach ($arrProperties as $objProperty) { |
352 | | $objProperty->setAccessible(true); |
353 | | $name = $objProperty->getName(); |
354 | | $arrResults[$name] = $objProperty->getValue($this); |
355 | | } |
356 | | // 親クラスがある場合は再帰的にプロパティを取得 |
357 | | $parentClass = $objReflect->getParentClass(); |
358 | | if (is_object($parentClass)) { |
359 | | $arr = self::toArray($parentClass); |
360 | | $arrResults = array_merge(self::toArray($parentClass), $arrResults); |
361 | | } |
362 | | return $arrResults; |
363 | | } |
| 346 | public function toArray(ReflectionClass $parentClass = null) { |
| 347 | $objReflect = null; |
| 348 | if (is_object($parentClass)) { |
| 349 | $objReflect = $parentClass; |
| 350 | } else { |
| 351 | $objReflect = new ReflectionClass($this); |
| 352 | } |
| 353 | $arrProperties = $objReflect->getProperties(); |
| 354 | foreach ($arrProperties as $objProperty) { |
| 355 | $objProperty->setAccessible(true); |
| 356 | $name = $objProperty->getName(); |
| 357 | $arrResults[$name] = $objProperty->getValue($this); |
| 358 | } |
| 359 | |
| 360 | // 親クラスがある場合は再帰的にプロパティを取得 |
| 361 | $parentClass = $objReflect->getParentClass(); |
| 362 | if (is_object($parentClass)) { |
| 363 | $arrParents = self::toArray($parentClass); |
| 364 | if (!is_array($arrParents)) { |
| 365 | $arrParents = array(); |
| 366 | } |
| 367 | if (!is_array($arrResults)) { |
| 368 | $arrResults = array(); |
| 369 | } |
| 370 | $arrResults = array_merge($arrParents, $arrResults); |
| 371 | } |
| 372 | return $arrResults; |
| 373 | } |