理想的牛 發表於 2022-6-2 13:54:00

PHP系列 | PHP中的stdClass是什么?

<p><span style="font-size: 16px"><strong>简介</strong></span></p>
<p><span style="font-size: 16px">stdClass 是 PHP 中的空类,用于将其他类型转换为对象。它类似于 Java 或 Python 对象。 stdClass 不是对象的基类</span></p>
<h3 class="title"><span style="font-size: 16px">转换为对象</span></h3>
<p><span style="font-size: 16px">如果将一个对象转换成对象,它将不会有任何变化。如果其它任何类型的值被转换成对象,将会创建一个内置类&nbsp;<code class="literal">stdClass</code>&nbsp;的实例。如果该值为&nbsp;<code>null</code>,则新的实例为空。&nbsp;<span class="type">array&nbsp;转换成&nbsp;<span class="type">object&nbsp;将使键名成为属性名并具有相对应的值。注意:在这个例子里, 使用 PHP 7.2.0 之前的版本,数字键只能通过迭代访问。</span></span></span></p>
<h2><span style="font-size: 16px">stdClass() 的定义</span></h2>
<p><span style="font-size: 16px">1、stdClass是PHP的一个基类,几乎所有的类都继承这个类,任何时候都可以被new,可以让一个变量成为一个对象(object)。</span></p>
<p><span style="font-size: 16px">2、所有使用 new stdClass 的变量,都不能使用方法,即不可能出现 $a-&gt;text() 的情况&nbsp;</span></p>
<p><span style="font-size: 16px">3、stdClass 在 php5 版本开始流行起来的,低于php5的版本,尽量或不使用此方法(好像这一条是多余的)</span></p>
<h2><span style="font-size: 16px">stdClass() 的用途</span></h2>
<p><span style="font-size: 16px">1、stdClass通过调用它们直接访问成员。</span></p>
<p><span style="font-size: 16px">2、它在动态对象中很有用。</span></p>
<p><span style="font-size: 16px">3、它用于设置动态属性等。</span></p>
<h2><span style="font-size: 16px">stdClass 类的使用</span></h2>
<p><span style="font-size: 16px"><strong>1、存储数据</strong></span></p>
<p><span style="font-size: 16px">(1)数组存储</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义数组存储个人信息</span>
<span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(128, 0, 128, 1)">$personal_array</span> =<span style="color: rgba(0, 0, 0, 1)"> [
</span><span style="color: rgba(0, 128, 128, 1)">3</span>   "name" =&gt; "Tinywan",
<span style="color: rgba(0, 128, 128, 1)">4</span>   "home" =&gt; "www.tinywan.com",
<span style="color: rgba(0, 128, 128, 1)">5</span>   "address" =&gt; "ZheJiang HangZhou"
<span style="color: rgba(0, 128, 128, 1)">6</span> <span style="color: rgba(0, 0, 0, 1)">];
</span><span style="color: rgba(0, 128, 128, 1)">7</span>
<span style="color: rgba(0, 128, 128, 1)">8</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 显示数组内容</span>
<span style="color: rgba(0, 128, 128, 1)">9</span> <span style="color: rgba(0, 128, 128, 1)">print_r</span>(<span style="color: rgba(128, 0, 128, 1)">$personal_array</span>);</span></pre>
</div>
<p><span style="font-size: 16px">输出内容</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 0, 255, 1)">Array</span><span style="color: rgba(0, 0, 0, 1)">
(
    </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> Tinywan
    </span>=&gt; www.tinywan.<span style="color: rgba(0, 0, 0, 1)">com   
    </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> ZheJiang HangZhou
)</span></span></pre>
</div>
<p><span style="font-size: 16px">(2)stdClass存储:使用 stdClass 而不是数组来存储个人详细信息(动态属性)</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(128, 0, 128, 1)">$personal_object</span> = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> stdClass();
</span><span style="color: rgba(128, 0, 128, 1)">$personal_object</span>-&gt;name = "Tinywan"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$personal_object</span>-&gt;home = "www.tinywan.com"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$personal_object</span>-&gt;address = "ZheJiang HangZhou"<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 显示对象内容</span>
<span style="color: rgba(0, 128, 128, 1)">print_r</span>(<span style="color: rgba(128, 0, 128, 1)">$personal_object</span>);</span></pre>
</div>
<p><span style="font-size: 16px">输出内容</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px">stdClass <span style="color: rgba(0, 0, 255, 1)">Object</span><span style="color: rgba(0, 0, 0, 1)">
(
    </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> Tinywan
    </span>=&gt; www.tinywan.<span style="color: rgba(0, 0, 0, 1)">com   
    </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> ZheJiang HangZhou
)</span></span></pre>
</div>
<p><span style="font-size: 16px">注意:数组到对象和对象到数组的类型转换是可能的。</span></p>
<p><span style="font-size: 16px">(3)将数组转换为对象</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(128, 0, 128, 1)">$personal_array</span> =<span style="color: rgba(0, 0, 0, 1)"> [
    </span>"name" =&gt; "Tinywan",
    "home" =&gt; "www.tinywan.com",
    "address" =&gt; "ZheJiang HangZhou"<span style="color: rgba(0, 0, 0, 1)">
];
</span><span style="color: rgba(128, 0, 128, 1)">$personal_object</span> = (<span style="color: rgba(0, 0, 255, 1)">object</span>) <span style="color: rgba(128, 0, 128, 1)">$personal_array</span><span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 显示对象内容</span>
<span style="color: rgba(0, 128, 128, 1)">print_r</span>(<span style="color: rgba(128, 0, 128, 1)">$personal_object</span>);</span></pre>
</div>
<p><span style="font-size: 16px">输出内容</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px">stdClass <span style="color: rgba(0, 0, 255, 1)">Object</span><span style="color: rgba(0, 0, 0, 1)">
(
    </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> Tinywan
    </span>=&gt; www.tinywan.<span style="color: rgba(0, 0, 0, 1)">com   
    </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> ZheJiang HangZhou
)</span></span></pre>
</div>
<p><span style="font-size: 16px">(4)将对象属性转换为数组</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(128, 0, 128, 1)">$personal_object</span> = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> stdClass();
</span><span style="color: rgba(128, 0, 128, 1)">$personal_object</span>-&gt;name = "Tinywan"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$personal_object</span>-&gt;home = "www.tinywan.com"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$personal_object</span>-&gt;address = "ZheJiang HangZhou"<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(128, 0, 128, 1)">$personal_array</span> = (<span style="color: rgba(0, 0, 255, 1)">array</span>) <span style="color: rgba(128, 0, 128, 1)">$personal_object</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 显示数组内容</span>
<span style="color: rgba(0, 128, 128, 1)">print_r</span>(<span style="color: rgba(128, 0, 128, 1)">$personal_array</span>);</span></pre>
</div>
<p><span style="font-size: 16px">输出内容</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 0, 255, 1)">Array</span><span style="color: rgba(0, 0, 0, 1)">
(
    </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> Tinywan
    </span>=&gt; www.tinywan.<span style="color: rgba(0, 0, 0, 1)">com
    </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> ZheJiang HangZhou
)</span></span></pre>
</div>
<p><span style="font-size: 16px"><strong>2、动态增加属性</strong></span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(128, 0, 128, 1)">$personal_object</span> = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> stdClass();
</span><span style="color: rgba(128, 0, 128, 1)">$personal_object</span>-&gt;name = "Tinywan"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$personal_object</span>-&gt;home = "www.tinywan.com"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$personal_object</span>-&gt;address = "ZheJiang HangZhou"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">print_r</span>(<span style="color: rgba(128, 0, 128, 1)">$personal_object</span><span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 动态增加属性</span>
<span style="color: rgba(128, 0, 128, 1)">$personal_object</span>-&gt;age = 24<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$personal_object</span>-&gt;schoole = "GanZhengFa"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">print_r</span>(<span style="color: rgba(128, 0, 128, 1)">$personal_object</span>);</span></pre>
</div>
<p><span style="font-size: 16px">输出</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px">stdClass <span style="color: rgba(0, 0, 255, 1)">Object</span><span style="color: rgba(0, 0, 0, 1)">
(
    </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> Tinywan
    </span>=&gt; www.tinywan.<span style="color: rgba(0, 0, 0, 1)">com
    </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> ZheJiang HangZhou
)
stdClass </span><span style="color: rgba(0, 0, 255, 1)">Object</span><span style="color: rgba(0, 0, 0, 1)">
(
    </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> Tinywan
    </span>=&gt; www.tinywan.<span style="color: rgba(0, 0, 0, 1)">com
    </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> ZheJiang HangZhou
    </span>=&gt; 24<span style="color: rgba(0, 0, 0, 1)">
    </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> GanZhengFa
)</span></span></pre>
</div>
<p><span style="font-size: 16px"><strong>&nbsp;stdClass 并不是 PHP 中对象的基类</strong></span></p>
<p><span style="font-size: 16px">这里需要注意的是,尽管是泛型类,stdClass 并不是 PHP 中对象的基类,我们可以使用 instanceof 关键字来证明这一点。</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Tinywan{

}

</span><span style="color: rgba(128, 0, 128, 1)">$objClass</span> = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Tinywan();
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(128, 0, 128, 1)">$objClass</span><span style="color: rgba(0, 0, 0, 1)"> instanceof \stdClass){
    </span><span style="color: rgba(0, 0, 255, 1)">echo</span> 'Yes'<span style="color: rgba(0, 0, 0, 1)">;
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 0, 255, 1)">echo</span> 'No'<span style="color: rgba(0, 0, 0, 1)">;
}</span></span></pre>
</div>
<p><span style="font-size: 16px">输出结果:No</span></p>
<p><span style="font-size: 16px">这阐明了 stdClass 不是 PHP 中对象的基类</span></p>
<p><span style="font-size: 16px"><strong>&nbsp;使用 json_encode() 和 json_decode()</strong></span></p>
<p><span style="font-size: 16px">json_encode() 和 json_decode() 是专门用于对 JSON 字符串执行操作的函数。json_encode() 用于将 Array 转换为 JSON字符串&nbsp;。因此,首先,我们将一个对象转换为 JSON 字符串,然后使用 json_decode() 将其转换为对象。</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(128, 0, 128, 1)">$empInfo</span> = <span style="color: rgba(0, 0, 255, 1)">array</span><span style="color: rgba(0, 0, 0, 1)">(
</span>'name'=&gt;'John',
'address'=&gt;'Houston',
'employment' =&gt; <span style="color: rgba(0, 0, 255, 1)">array</span><span style="color: rgba(0, 0, 0, 1)">(
    </span>'id' =&gt; '1',
    'address' =&gt; 'Los Angeles'<span style="color: rgba(0, 0, 0, 1)">
    )
);
</span><span style="color: rgba(0, 128, 128, 1)">print_r</span>(json_decode(json_encode(<span style="color: rgba(128, 0, 128, 1)">$empInfo</span>)));</span></pre>
</div>
<p><span style="font-size: 16px">输出结果</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px">stdClass <span style="color: rgba(0, 0, 255, 1)">Object</span><span style="color: rgba(0, 0, 0, 1)">
(
    </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> John
    </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> Houston
    </span>=&gt; stdClass <span style="color: rgba(0, 0, 255, 1)">Object</span><span style="color: rgba(0, 0, 0, 1)">
      (
          </span>=&gt; 1<span style="color: rgba(0, 0, 0, 1)">
          </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> Los Angeles
      )
)</span></span></pre>
</div>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    <div>
    <p>作者:Tinywan</p>
</div>
<div>
    <p>出处:https://www.cnblogs.com/Tinywan</p>
</div>
<div>
    <p>本文版权归作者和博客园共有。欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。</p>
</div>
<div>
    <p>欢迎关注个人微信公众号,一起进步!扫描左方二维码即可</p>
</div><br><br>
来源:https://www.cnblogs.com/tinywan/p/16337537.html
頁: [1]
查看完整版本: PHP系列 | PHP中的stdClass是什么?