admin 发表于 2020-11-22 15:17:03

ThinkPHP,八、session会话作为模型对象使用,用于传递对象...


<h1 style="margin-top: 10px; line-height: 32px; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, Verdana, 微软雅黑;"><font size="5">先吐槽一下,大家千万不要买&nbsp; 夏磊写的ThinkPHP实战这本书,写的奇烂无比,而且原书附的源码错漏百出。本文章的这个问题就是这本书中源码的错误,把模型作为会话,用会话传递模型对象,在官网上加V程序员回复这是错误用法,而这本书的作者就这么用,所以源码一用就报错。没见过这么烂的书。</font></h1><p><font size="4"><br></font></p><p></p><h1 style="margin-top: 10px; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, Verdana, 微软雅黑; line-height: 32px;"><font size="4">下面是错误提示:</font></h1><h1 style="margin-top: 10px; line-height: 32px; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, Verdana, 微软雅黑;"><font size="4">method_exists(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "app\common\model\User" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide an autoloader to load the class definition</font></h1><p></p><h1 style="margin-top: 10px; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, Verdana, 微软雅黑; line-height: 32px;"><font size="4">源代码的逻辑是这样的:</font></h1><p></p><p><font size="4">&nbsp;const SESSION_KEY = 'user';&nbsp; &nbsp;定义常量</font></p><p><font size="4">$user = User::get($userId);&nbsp; &nbsp;模型查询表<br></font></p><p><font size="4">&nbsp;session(self::SESSION_KEY, $user);&nbsp; 把模型赋值给会话</font></p><p><font size="4">&nbsp;$user = session(self::SESSION_KEY);&nbsp; 在其他方法里再用会话传递还原模型对象<br></font></p><p></p><h1 style="margin-top: 10px; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, Verdana, 微软雅黑; line-height: 32px;"><font size="4">这作者想的是挺简单,但模型对象是不能转换为会话,用会话传递模型对象是十几年前的作法。</font></h1><p><font size="4"><br></font></p><p></p><p></p><h1 style="margin-top: 10px; line-height: 32px;"><font size="4"><span style="font-family: Consolas, &quot;Liberation Mono&quot;, Courier, Verdana, 微软雅黑;">在PHP升级到5.2以后,这样是不被支持的,临时的解决方案是,序列化传递过来的模型对象,先暂时用着。代码如下:</span><font face="Consolas, Liberation Mono, Courier, Verdana, 微软雅黑">&nbsp;</font></font></h1><h1 style="margin-top: 10px; line-height: 32px;"><font face="Consolas, Liberation Mono, Courier, Verdana, 微软雅黑" size="4">$user = session(self::SESSION_KEY);</font></h1><h1 style="margin-top: 10px; line-height: 32px;"><font face="Consolas, Liberation Mono, Courier, Verdana, 微软雅黑" size="4">$user=serialize($user);</font></h1><p><br></p><p><br></p>
页: [1]
查看完整版本: ThinkPHP,八、session会话作为模型对象使用,用于传递对象...