感觉好久没写东西了。一方面,我的角色发生了变化。每天都要面对各种各样的事情和突发事件,不能有整整很长的时间让自己静下心来写代码,或者写文章。另一方面,公司的技术栈不再仅限于Laravel+VUE。我们也开发小程序、APP等,所以比较看重东西。以后我会继续“高产”,把写技术文章当成一种习惯,坚持下去。好了,废话不多说,今天就来说说《Eloquent:Modifier》吧。一直想学Eloquent。但苦于Eloquent研究太多,找不到切入点。前两天有个同事好像对这个《Eloquent:Modifier》知之甚少,所以今天就把它当作一个入门,看了一下它的源码。首先以一个Demo为例:Demobirthday);返回Carbon::now()->diffInYears($date);}}这段代码比较简单,就是通过已有的属性birthday,计算出Baby几岁,得到age属性。前端可以直接得到结果:return$baby->age;同样,有一个setXxxAttribute方法来定义修饰符。源码阅读代码还是从使用入手。如上,通过$baby->age调用age属性。该属性在类中没有定义,只能通过PHP的魔术方法__get()来调用。让我们看看Model类的__get()方法:/***动态检索模型的属性。**@paramstring$key*@returnmixed*/publicfunction__get($key){return$this->getAttribute($key);}好吧,让我们看一下源码:/***从模型。**@paramstring$key*@returnmixed*/publicfunctiongetAttribute($key){if(!$key){返回;}//如果属性存在于属性数组中或具有“get”变元,我们将//获取属性的值。否则,我们将像开发人员//要求关系的价值一样进行。这涵盖了两种类型的值。if(array_key_exists($key,$this->attributes)||$this->hasGetMutator($key)){返回$this->getAttributeValue($key);}...}重点自然是第二个if,主要判断属性是否包含在attributes数组中。如果没有,函数$this->hasGetMutator($key)将被执行:/***确定属性是否存在get修改器。**@paramstring$key*@returnbool*/publicfunction哈sGetMutator($key){returnmethod_exists($this,'get'.Str::studly($key).'Attribute');}这个匹配我们Demo中的自定义函数getAgeAttribute(),即返回true后,下一步是执行函数$this->getAttributeValue($key),然后执行函数:return$this->mutateAttribute($key,$value);/***使用其修改器获取属性的值.**@paramstring$key*@parammixed$value*@returnmixed*/protectedfunctionmutateAttribute($key,$value){return$this->{'get'.Str::studly($key).'Attribute'}($value);}好了,现在我们基本知道获取自定义Attribute的流程了。我相信解析setXxxAttribute也很简单。总结一下,好久没写东西了。让我们从最简单的开始练习。解析Eloquent需要大量的脑细胞。在接下来的一段时间里,我会仔细研究这个话题,并尽可能地解读它:.|____Capsule||____Manager.php|____composer.json|____Concerns||____BuildsQueries.php|____ManagesTransactions.php|____Connection.php|____ConnectionInterface.php|____ConnectionResolver.php|____ConnectionResolverInterface.php|____Connectors||____ConnectionFactory.php||____Connector.php||____ConnectorInterface.php||____MySqlConnector.php||____PostgresConnector.php||____SQLiteConnector.php||____SqlServerConnector.php|____控制台||____工厂|||____FactoryMakeCommand.php|||____存根||||____factory.stub||____迁移|||____BaseCommand.php||PHP|||____MigrateCommand.php|||____MigrateMakeCommand.php|||____RefreshCommand.php|||____ResetCommand.php|||____RollbackCommand.php|||____StatusCommand.php|.php|||____存根||||____seeder.stub|____DatabaseManager.php|____DatabaseServiceProvider.php|____DetectsDeadlocks.php|____DetectsLostConnections.php|____Eloquent||____Builder.php||____Collection.php||____关注|||____GuardsAttributes.php|||____HasAttributes.php|||____HasEvents.php|||____HasGlobalScopes.php|||____HasRelationships.php|||____HasTimestamps.php|||____HidesAttributes.php|||____QueriesRelationships.php||____工厂.php||____FactoryBuilder.php||____JsonEncodingException.php||____MassAssignmentException.php||____Model.php||____ModelNotFoundException.php||____QueueEntityResolver.php||____RelationNotFoundException.php||____关系|||____BelongsTo.php|||____BelongsToMany.php|||____关注||||____InteractsWithPivotTable.php||||____SupportsDefaultModels.php|||____HasMany.php|||____HasManyThrough.php|||____HasOne.php|||____HasOneOrMany.php|||____MorphMany.php|||____MorphOne.php|||____MorphOneOrMany.php|||____MorphPivot.php|||____MorphTo.php|||____MorphToMany.php|||____Pivot.php|||____Relation.php||____范围.php||____SoftDeletes.php||____SoftDeletingScope.php|____事件||____ConnectionEvent.php||____QueryExecuted.php||____StatementPrepared.php||____TransactionBeginning.php||____TransactionCommitted.php||____TransactionRolledBack.php|____Grammar.php|____Migrations||____DatabaseMigrationRepository.php||____迁移.php||____MigrationCreator.php||____MigrationRepositoryInterface.php||____Migrator.php||____存根|||____空白存根|||____create.stub|||____update.stub|____MigrationServiceProvider.php|____MySqlConnection.php|____PostgresConnection.php|____Query||____Builder.php||____Expression.php||____语法|||____语法.php|||____MySqlGrammar.php|||____PostgresGrammar.php|||____SQLiteGrammar.php|||____SqlServerGrammar.php||____JoinClause.php||____JsonExpression.php||____处理器|||____MySqlProcessor.php|||____PostgresProcessor.php|||____处理器.php|||____SQLiteProcessor.php|||____SqlServerProcessor.php|____QueryException.php|____README.md|____Schema||____蓝图.php||____Builder.php|||____MySqlGrammar.php|||____PostgresGrammar.php||____RenameColumn.php|||____SQLiteGrammar.php||____Seeder.php参考Eloquent:Modifierhttps://laravel-china.org/docs/laravel/5.7/eloquent-mutators/2297__get()指令http://php.net/manual/zh/language.oop5。overloading.php#object.get待续
