laravel 中使用反射得到类的所有方法

Posted 独孤剑—宇枫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel 中使用反射得到类的所有方法相关的知识,希望对你有一定的参考价值。

以Carbon这个时间处理库为例

$class = new ReflectionClass(‘CarbonCarbon‘);//获取反射类
        $methods = $class->getMethods();//获取反射类的所有的方法
        //各个方法的处理效果
        foreach ($methods as $key => $value) {
            $hasMethod[] = $value->name;
            if (ends_with($value->name, ‘String‘) && starts_with($value->name, ‘to‘))
            {
                echo $value->name . ‘() ---输出格式为:‘ . php_EOL;
                echo Carbon::now()->{$value->name}() . PHP_EOL;
            }
        }
        dd($hasMethod);

结果:

toDateString() ---输出格式为:
2018-12-06
toFormattedDateString() ---输出格式为:
Dec 6, 2018
toTimeString() ---输出格式为:
11:23:45
toDateTimeString() ---输出格式为:
2018-12-06 11:23:45
toDayDateTimeString() ---输出格式为:
Thu, Dec 6, 2018 11:23 AM
toAtomString() ---输出格式为:
2018-12-06T11:23:45+08:00
toCookieString() ---输出格式为:
Thursday, 06-Dec-2018 11:23:45 CST
toIso8601String() ---输出格式为:
2018-12-06T11:23:45+08:00
toRfc822String() ---输出格式为:
Thu, 06 Dec 18 11:23:45 +0800
toIso8601ZuluString() ---输出格式为:
2018-12-06T03:23:45Z
toRfc850String() ---输出格式为:
Thursday, 06-Dec-18 11:23:45 CST
toRfc1036String() ---输出格式为:
Thu, 06 Dec 18 11:23:45 +0800
toRfc1123String() ---输出格式为:
Thu, 06 Dec 2018 11:23:45 +0800
toRfc2822String() ---输出格式为:
Thu, 06 Dec 2018 11:23:45 +0800
toRfc3339String() ---输出格式为:
2018-12-06T11:23:45+08:00
toRssString() ---输出格式为:
Thu, 06 Dec 2018 11:23:45 +0800
toW3cString() ---输出格式为:
2018-12-06T11:23:45+08:00
toRfc7231String() ---输出格式为:
Thu, 06 Dec 2018 03:23:45 GMT



array:279 [
  0 => "useMicrosecondsFallback"
  1 => "isMicrosecondsFallbackEnabled"
  2 => "useMonthsOverflow"
  3 => "resetMonthsOverflow"
  4 => "shouldOverflowMonths"
  5 => "useYearsOverflow"
  6 => "resetYearsOverflow"
  7 => "shouldOverflowYears"
  8 => "safeCreateDateTimeZone"
  9 => "__construct"
  10 => "instance"
  11 => "parse"
  12 => "now"
  13 => "today"
  14 => "tomorrow"
  15 => "yesterday"
  16 => "maxValue"
  17 => "minValue"
  18 => "create"
  19 => "createSafe"
  20 => "createFromDate"
  21 => "createMidnightDate"
  22 => "createFromTime"
  23 => "createFromTimeString"
  24 => "createFromFormat"
  25 => "setLastErrors"
  26 => "getLastErrors"
  27 => "createFromTimestamp"
  28 => "createFromTimestampMs"
  29 => "createFromTimestampUTC"
  30 => "copy"
  31 => "nowWithSameTz"
  32 => "resolveCarbon"
  33 => "__get"
  34 => "__isset"
  35 => "__set"
  36 => "year"
  37 => "month"
  38 => "day"
  39 => "hour"
  40 => "minute"
  41 => "second"
  42 => "setDate"
  43 => "setDateTime"
  44 => "setTimeFromTimeString"
  45 => "timestamp"
  46 => "timezone"
  47 => "tz"
  48 => "setTimezone"
  49 => "getDays"
  50 => "getWeekStartsAt"
  51 => "setWeekStartsAt"
  52 => "getWeekEndsAt"
  53 => "setWeekEndsAt"
  54 => "getWeekendDays"
  55 => "setWeekendDays"
  56 => "getMidDayAt"
  57 => "setMidDayAt"
  58 => "setTestNow"
  59 => "getTestNow"
  60 => "hasTestNow"
  61 => "hasRelativeKeywords"
  62 => "translator"
  63 => "getTranslator"
  64 => "setTranslator"
  65 => "getLocale"
  66 => "setLocale"
  67 => "setUtf8"
  68 => "formatLocalized"
  69 => "resetToStringFormat"
  70 => "setToStringFormat"
  71 => "__toString"
  72 => "toDateString"
  73 => "toFormattedDateString"
  74 => "toTimeString"
  75 => "toDateTimeString"
  76 => "toDayDateTimeString"
  77 => "toAtomString"
  78 => "toCookieString"
  79 => "toIso8601String"
  80 => "toRfc822String"
  81 => "toIso8601ZuluString"
  82 => "toRfc850String"
  83 => "toRfc1036String"
  84 => "toRfc1123String"
  85 => "toRfc2822String"
  86 => "toRfc3339String"
  87 => "toRssString"
  88 => "toW3cString"
  89 => "toRfc7231String"
  90 => "toArray"
  91 => "eq"
  92 => "equalTo"
  93 => "ne"
  94 => "notEqualTo"
  95 => "gt"
  96 => "greaterThan"
  97 => "gte"
  98 => "greaterThanOrEqualTo"
  99 => "lt"
  100 => "lessThan"
  101 => "lte"
  102 => "lessThanOrEqualTo"
  103 => "between"
  104 => "closest"
  105 => "farthest"
  106 => "min"
  107 => "minimum"
  108 => "max"
  109 => "maximum"
  110 => "isWeekday"
  111 => "isWeekend"
  112 => "isYesterday"
  113 => "isToday"
  114 => "isTomorrow"
  115 => "isNextWeek"
  116 => "isLastWeek"
  117 => "isNextMonth"
  118 => "isLastMonth"
  119 => "isNextYear"
  120 => "isLastYear"
  121 => "isFuture"
  122 => "isPast"
  123 => "isLeapYear"
  124 => "isLongYear"
  125 => "isSameAs"
  126 => "isCurrentYear"
  127 => "isSameYear"
  128 => "isCurrentMonth"
  129 => "isSameMonth"
  130 => "isSameDay"
  131 => "isDayOfWeek"
  132 => "isSunday"
  133 => "isMonday"
  134 => "isTuesday"
  135 => "isWednesday"
  136 => "isThursday"
  137 => "isFriday"
  138 => "isSaturday"
  139 => "hasFormat"
  140 => "addCenturies"
  141 => "addCentury"
  142 => "subCenturies"
  143 => "subCentury"
  144 => "addYears"
  145 => "addYear"
  146 => "addYearsNoOverflow"
  147 => "addYearNoOverflow"
  148 => "addYearsWithOverflow"
  149 => "addYearWithOverflow"
  150 => "subYears"
  151 => "subYear"
  152 => "subYearsNoOverflow"
  153 => "subYearNoOverflow"
  154 => "subYearsWithOverflow"
  155 => "subYearWithOverflow"
  156 => "addQuarters"
  157 => "addQuarter"
  158 => "subQuarters"
  159 => "subQuarter"
  160 => "addMonths"
  161 => "addMonth"
  162 => "subMonths"
  163 => "subMonth"
  164 => "addMonthsWithOverflow"
  165 => "addMonthWithOverflow"
  166 => "subMonthsWithOverflow"
  167 => "subMonthWithOverflow"
  168 => "addMonthsNoOverflow"
  169 => "addMonthNoOverflow"
  170 => "subMonthsNoOverflow"
  171 => "subMonthNoOverflow"
  172 => "addDays"
  173 => "addDay"
  174 => "subDays"
  175 => "subDay"
  176 => "addWeekdays"
  177 => "addWeekday"
  178 => "subWeekdays"
  179 => "subWeekday"
  180 => "addWeeks"
  181 => "addWeek"
  182 => "subWeeks"
  183 => "subWeek"
  184 => "addHours"
  185 => "addRealHours"
  186 => "addHour"
  187 => "addRealHour"
  188 => "subHours"
  189 => "subRealHours"
  190 => "subHour"
  191 => "subRealHour"
  192 => "addMinutes"
  193 => "addRealMinutes"
  194 => "addMinute"
  195 => "addRealMinute"
  196 => "subMinute"
  197 => "subRealMinute"
  198 => "subMinutes"
  199 => "subRealMinutes"
  200 => "addSeconds"
  201 => "addRealSeconds"
  202 => "addSecond"
  203 => "addRealSecond"
  204 => "subSeconds"
  205 => "subRealSeconds"
  206 => "subSecond"
  207 => "subRealSecond"
  208 => "diffInYears"
  209 => "diffInMonths"
  210 => "diffInWeeks"
  211 => "diffInDays"
  212 => "diffInDaysFiltered"
  213 => "diffInHoursFiltered"
  214 => "diffFiltered"
  215 => "diffInWeekdays"
  216 => "diffInWeekendDays"
  217 => "diffInHours"
  218 => "diffInRealHours"
  219 => "diffInMinutes"
  220 => "diffInRealMinutes"
  221 => "diffInSeconds"
  222 => "diffInRealSeconds"
  223 => "secondsSinceMidnight"
  224 => "secondsUntilEndOfDay"
  225 => "diffForHumans"
  226 => "startOfDay"
  227 => "endOfDay"
  228 => "startOfMonth"
  229 => "endOfMonth"
  230 => "startOfQuarter"
  231 => "endOfQuarter"
  232 => "startOfYear"
  233 => "endOfYear"
  234 => "startOfDecade"
  235 => "endOfDecade"
  236 => "startOfCentury"
  237 => "endOfCentury"
  238 => "startOfWeek"
  239 => "endOfWeek"
  240 => "startOfHour"
  241 => "endOfHour"
  242 => "startOfMinute"
  243 => "endOfMinute"
  244 => "midDay"
  245 => "next"
  246 => "nextOrPreviousDay"
  247 => "nextWeekday"
  248 => "previousWeekday"
  249 => "nextWeekendDay"
  250 => "previousWeekendDay"
  251 => "previous"
  252 => "firstOfMonth"
  253 => "lastOfMonth"
  254 => "nthOfMonth"
  255 => "firstOfQuarter"
  256 => "lastOfQuarter"
  257 => "nthOfQuarter"
  258 => "firstOfYear"
  259 => "lastOfYear"
  260 => "nthOfYear"
  261 => "average"
  262 => "isBirthday"
  263 => "isLastOfMonth"
  264 => "serialize"
  265 => "fromSerialized"
  266 => "__set_state"
  267 => "__wakeup"
  268 => "format"
  269 => "modify"
  270 => "add"
  271 => "sub"
  272 => "getTimezone"
  273 => "getOffset"
  274 => "setTime"
  275 => "setISODate"
  276 => "setTimestamp"
  277 => "getTimestamp"
  278 => "diff"
]

IOC是一种思想,DI是实现IOC的一种方法
通过类名与方法,根据反射来实现

$class = new ReflectionClass(‘CarbonCarbon‘);//获取反射类
$obj = $class->newInstance();//实例化对象
echo $obj->now()->toDateString() . PHP_EOL;
echo $obj->now()->toDateTimeString() . PHP_EOL;
echo $obj->now()->toTimeString() . PHP_EOL;```
结果:

2018-12-06
2018-12-06 11:35:41
11:35:41
```





以上是关于laravel 中使用反射得到类的所有方法的主要内容,如果未能解决你的问题,请参考以下文章

java反射基础知识反射应用实践

注解和反射的demo

PHP 反射的简单使用

Java 反射机制

Laravel:如何在控制器的几种方法中重用代码片段

什么是反射机制