yxrj 发表于 2020-3-20 19:26:06

PHP判断{函数/类/方法/属性}是否存在

<pre style="color: rgb(0, 0, 0); font-size: 12px; text-align: justify; white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><p style="margin: 10px auto; color: rgb(94, 94, 94); font-family: Verdana, Helvetica, Arial; font-size: 13px; white-space: normal;">1.php判断系统函数或自己写的函数是否存在</p><p style="margin: 10px auto; color: rgb(94, 94, 94); font-family: Verdana, Helvetica, Arial; font-size: 13px; white-space: normal;">bool function_exists ( string $function_name ) 判断函数是否已经定义,例如:</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin-top: 5px; margin-bottom: 5px; white-space: normal;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span>(<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">function_exists</span>('curl_init'<span style="line-height: 1.5 !important;">)){
    curl_init();
}</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">else</span><span style="line-height: 1.5 !important;">{
    </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">echo</span> 'not function curl_init'<span style="line-height: 1.5 !important;">;
}</span></pre></div><p style="margin: 10px auto; color: rgb(94, 94, 94); font-family: Verdana, Helvetica, Arial; font-size: 13px; white-space: normal;">2.php判断类是否存在</p><p style="margin: 10px auto; color: rgb(94, 94, 94); font-family: Verdana, Helvetica, Arial; font-size: 13px; white-space: normal;">bool class_exists ( string $class_name [, bool $autoload = true ] ) 检查一个类是否已经定义,一定以返回true,否则返回false,例如:</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin-top: 5px; margin-bottom: 5px; white-space: normal;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span>(<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">class_exists</span>('MySQL'<span style="line-height: 1.5 !important;">)){
    </span><span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$myclass</span>=<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">new</span> <span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">MySQL</span><span style="line-height: 1.5 !important;">();
}</span></pre></div><p style="margin: 10px auto; color: rgb(94, 94, 94); font-family: Verdana, Helvetica, Arial; font-size: 13px; white-space: normal;">3.php判断类里面的某个方法是否已经定义</p><p style="margin: 10px auto; color: rgb(94, 94, 94); font-family: Verdana, Helvetica, Arial; font-size: 13px; white-space: normal;">bool method_exists ( mixed $object , string $method_name ) 检查类的方法是否存在,例如:</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin-top: 5px; margin-bottom: 5px; white-space: normal;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$directory</span>=<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">new</span><span style="line-height: 1.5 !important;"> Directory;
</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span>(!<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">method_exists</span>(<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$directory</span>,'read'<span style="line-height: 1.5 !important;">)){
    </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">echo</span> '未定义read方法!'<span style="line-height: 1.5 !important;">;
}</span></pre></div><p style="margin: 10px auto; color: rgb(94, 94, 94); font-family: Verdana, Helvetica, Arial; font-size: 13px; white-space: normal;">4.php 判断类里面的某个属性是否已经定义</p><p style="margin: 10px auto; color: rgb(94, 94, 94); font-family: Verdana, Helvetica, Arial; font-size: 13px; white-space: normal;"><span class="type">bool&nbsp;<span class="methodname">property_exists&nbsp;(&nbsp;<span class="methodparam"><span class="type">mixed&nbsp;<code class="parameter">$class</code>&nbsp;,&nbsp;<span class="methodparam"><span class="type">string&nbsp;<code class="parameter">$property</code>&nbsp;)检查类的属性是否存在,例如:</span></span></span></span></span></span></p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin-top: 5px; margin-bottom: 5px; white-space: normal;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$directory</span>=<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">new</span><span style="line-height: 1.5 !important;"> Directory;
</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span>(!property_exists(<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$directory</span>,'li'<span style="line-height: 1.5 !important;">)){
    </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">echo</span> '未定义li属性!'<span style="line-height: 1.5 !important;">;
}</span></pre></div><p style="margin: 10px auto; color: rgb(94, 94, 94); font-family: Verdana, Helvetica, Arial; font-size: 13px; white-space: normal;">&nbsp;</p></pre><p></p>
页: [1]
查看完整版本: PHP判断{函数/类/方法/属性}是否存在