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

以Carbon这个时间处理库为例

$class = new \ReflectionClass(‘Carbon\Carbon‘);//获取反射类
        $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(‘Carbon\Carbon‘);//获取反射类
$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

```

原文地址:https://www.cnblogs.com/xzj8023tp/p/10075993.html

时间: 2024-10-11 01:08:52

laravel 中使用反射得到类的所有方法的相关文章

通过反射 调用 类里面的 方法的方法

通过反射 调用 类里面的 方法的方法(注意 重载 ,可以 按F12转到 定义 查看 ): 错误方法: MethodInfo dynMethod = this.GetType().GetMethod("Draw_" + itemType); dynMethod.Invoke(this, new object[] { methodParams }); 正确 方法 : MethodInfo dynMethod = this.GetType().GetMethod("Draw_&qu

C#如何通过反射调用类下的方法

首先模拟一个mvc的项目,创建一个业务类(HomeService),在类下创建3个方法 public class HomeService { /// <summary> /// 无参方法 /// </summary> /// <returns></returns> public string SayHello() { string str = "你好!"; return str; } /// <summary> /// 有参方

C#中通过反射获取类中非公有成员

public class NGlbGlobeXComm { public static T GetPrivateField<T>(object instance, string fieldname)            {                                BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;                Type type = instance.G

Java中Date和Calender类的使用方法

查看文章     2009-10-04 20:49 Date和Calendar是Java类库里提供对时间进行处理的类,由于日期在商业逻辑的应用中占据着很重要的地位,所以在这里想对这两个类进行一个基本的讲解,由于技术有限,不到之处请指正. Date类顾名思义,一看就知道是和日期有关的类了,这个类最主要的作用就是获得当前时间了,然而这个类里面也具有设置时间以及一些其他的功能,可是由于 本身设计的问题,这些方法却遭到众多批评,而这些遭受批评的功能都已移植到另外一个类里面,这就是今天要讲到的第二个类Ca

D语言中使用extern(Windows)类重载Object方法时的问题

使用D语言已经有快20天了,已经完成项目需要的基本功能,但还需进一步完善. 最近两三天在DGUI基础上移植了几个Window窗口控件,有TreeList(树+列表视图),DataList(数据列表),MdiFrom(多窗口),IEWebBrowser(IE浏览器),SciEditor(Sci源代码编辑器),DockPanel(notpad的Docking插件),SysHeader(表头控件), SysLink等几个标准Windows控件. 感谢DGUI的作者,对Windows控件做的布局管理及封

浅谈java中的对象、类、与方法的重载

对象: 一切皆为对象. 对象包括两部分内容:属性(名词形容词),行为(动词). 对象和对象之间是有关系的: 派生,关联,依赖. 类: 对同一类别的众多对象的一种抽象. 类,还是用来生成对象的一种模板,对象是类的一种具体化的表现. 面向对象的三大特性:封装,继承,多态. ? 1 2 3 4 class 类名{ 访问修饰符 成员变量的定义; 访问修饰符 成员函数(方法)的定义; } 访问修改符:默认不写,private,public. private,私有.只能被当前class 类名{}中的代码访问

反射和类内置方法

# class Teacher: # dic = {'查看学生信息': 'show_student', '查看老师': 'show_teacher'} # # def show_student(self): # print('show student') # # def show_teacher(self): # print('show teacher') # # @classmethod # def func(cls): # print('func') # # chen = Teacher()

C#反射调用类的私有方法

void Main() { var type = typeof(StockClass); type.Dump(); var fields=type.GetFields(BindingFlags.Instance|BindingFlags.NonPublic).Dump("字段"); var members = type.GetMethods().Dump("方法"); //调用私有的构造函数,获得实例化对象 object obj=type.GetConstructo

java中的反射机制在Android开发中的用处

JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制.      然而在android中Google很多的类的某些方法不让第三方应用去调用,通过java反射机制能把这些隐藏方法获取出来并调用,三方应用上我们就很方便的去用这些方法.      例如我们需要安全的杀死某个应用的服务和进程调用ActivityManager.forceStopPack