引言
感觉好长时间没写东西了,一方面主要是自己的角色发生了变化,每天要面对各种各样的事情和突发事件,不能再有一个完整的长时间让自己静下来写代码,或者写文章。
另一方面现在公司技术栈不再停留在只有 laravel + vue 了,我们还有小程序、app 等开发,所以我关注的东西也就多了。
接下来我还是会继续持续「高产」,把写技术文章当作一个习惯,坚持下去。
好了,废话不多说,今天来说一说「eloquent: 修改器
」。
一直想好好研究下 eloquent。但苦于 eloquent 有太多可研究的,无法找到一个切入点。前两天看一同事好像对这个「eloquent: 修改器
」了解不多,所以今天就拿它作为入口,扒一扒其实现源代码。
首先还是拿一个 demo 为例:
demo
<?php namespace app\models; use illuminate\database\eloquent\model; use carbon\carbon; class baby extends model { protected $table = 'baby'; protected $appends = ['age']; public function getageattribute() { $date = new carbon($this->birthday); return carbon::now()->diffinyears($date); } }
这个代码比较简单,就是通过已有属性 birthday
,计算 baby 几岁了,得到 age
属性。
前端就可以直接拿到结果:
return $baby->age;
同样的,还有 setxxxattribute
方法来定义一个修改器。
源代码
读代码还是从使用入手,如上通过 $baby->age
调用 age
属性,这个属性没在类中定义,所以只能通过 php 的魔术方法 __get()
调用了。
我们看看 model
类的 __get()
方法:
/** * dynamically retrieve attributes on the model. * * @param string $key * @return mixed */ public function __get($key) { return $this->getattribute($key); }
好了,我们开始解读源代码了:
/** * get an attribute from the model. * * @param string $key * @return mixed */ public function getattribute($key) { if (! $key) { return; } // if the attribute exists in the attribute array or has a "get" mutator we will // get the attribute's value. otherwise, we will proceed as if the developers // are asking for a relationship's value. this covers both types of values. if (array_key_exists($key, $this->attributes) || $this->hasgetmutator($key)) { return $this->getattributevalue($key); } ... }
重点自然就在第二个 if
上,主要判断 attributes
数组中是否包含该属性,如果没有,则会执行函数 $this->hasgetmutator($key)
:
/** * determine if a get mutator exists for an attribute. * * @param string $key * @return bool */ public function hasgetmutator($key) { return method_exists($this, 'get'.str::studly($key).'attribute'); }
这就对上了我们的 demo
中自定义的函数 getageattribute()
,也就返回 true
了。
接下来就是执行函数 $this->getattributevalue($key)
,进而执行函数:return $this->mutateattribute($key, $value);
/** * get the value of an attribute using its mutator. * * @param string $key * @param mixed $value * @return mixed */ protected function mutateattribute($key, $value) { return $this->{'get'.str::studly($key).'attribute'}($value); }
好了,到此我们基本就知道了获取自定义 attribute
的流程了。
相信解析 set xxxattribute
也是很简单的。
总结
好长时间没写东西了,先从最简单的入手,练练手。解析 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 |____console | |____factories | | |____factorymakecommand.php | | |____stubs | | | |____factory.stub | |____migrations | | |____basecommand.php | | |____freshcommand.php | | |____installcommand.php | | |____migratecommand.php | | |____migratemakecommand.php | | |____refreshcommand.php | | |____resetcommand.php | | |____rollbackcommand.php | | |____statuscommand.php | |____seeds | | |____seedcommand.php | | |____seedermakecommand.php | | |____stubs | | | |____seeder.stub |____databasemanager.php |____databaseserviceprovider.php |____detectsdeadlocks.php |____detectslostconnections.php |____eloquent | |____builder.php | |____collection.php | |____concerns | | |____guardsattributes.php | | |____hasattributes.php | | |____hasevents.php | | |____hasglobalscopes.php | | |____hasrelationships.php | | |____hastimestamps.php | | |____hidesattributes.php | | |____queriesrelationships.php | |____factory.php | |____factorybuilder.php | |____jsonencodingexception.php | |____massassignmentexception.php | |____model.php | |____modelnotfoundexception.php | |____queueentityresolver.php | |____relationnotfoundexception.php | |____relations | | |____belongsto.php | | |____belongstomany.php | | |____concerns | | | |____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 | |____scope.php | |____softdeletes.php | |____softdeletingscope.php |____events | |____connectionevent.php | |____queryexecuted.php | |____statementprepared.php | |____transactionbeginning.php | |____transactioncommitted.php | |____transactionrolledback.php |____grammar.php |____migrations | |____databasemigrationrepository.php | |____migration.php | |____migrationcreator.php | |____migrationrepositoryinterface.php | |____migrator.php | |____stubs | | |____blank.stub | | |____create.stub | | |____update.stub |____migrationserviceprovider.php |____mysqlconnection.php |____postgresconnection.php |____query | |____builder.php | |____expression.php | |____grammars | | |____grammar.php | | |____mysqlgrammar.php | | |____postgresgrammar.php | | |____sqlitegrammar.php | | |____sqlservergrammar.php | |____joinclause.php | |____jsonexpression.php | |____processors | | |____mysqlprocessor.php | | |____postgresprocessor.php | | |____processor.php | | |____sqliteprocessor.php | | |____sqlserverprocessor.php |____queryexception.php |____readme.md |____schema | |____blueprint.php | |____builder.php | |____grammars | | |____changecolumn.php | | |____grammar.php | | |____mysqlgrammar.php | | |____postgresgrammar.php | | |____renamecolumn.php | | |____sqlitegrammar.php | | |____sqlservergrammar.php | |____mysqlbuilder.php | |____postgresbuilder.php | |____sqlitebuilder.php | |____sqlserverbuilder.php |____seeder.php
参考
- eloquent: 修改器
- __get()使用说明 http://php.net/manual/zh/language.oop5.overloading.php#object.get
以上就是php学习eloquent修改器源码示例解析的详细内容,更多关于php eloquent修改器的资料请关注代码网其它相关文章!
发表评论