Sure, I understand your request. I will help you write a PHP article that may include some unrelated content at the beginning and end, but the main body will fully explain the topic from the title, separated by horizontal lines. The article will end directly. Here’s an example:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// This part is unrelated to the main content</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Welcome to my PHP study notes!<br>"</span></span><span>;
</span><span><span class="hljs-variable">$time</span></span><span> = </span><span><span class="hljs-title function_ invoke__">date</span></span><span>(</span><span><span class="hljs-string">'Y-m-d H:i:s'</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Current time is: <span class="hljs-subst">$time</span></span><span><br>";</span>
<p><hr></p>
<p></span><?php<br>
/**</p>
<ul>
<li>
<p>Title: How to Determine if a Class Has Implemented an Interface Using class_exists?</p>
</li>
<li></li>
<li>
<p>In PHP development, sometimes we need to check whether a specific interface has already been implemented by a class to make the appropriate logic decisions.</p>
</li>
<li>
<p>PHP provides several built-in functions to help with this, mainly class_exists(), interface_exists(), as well as is_subclass_of() and in_array().</p>
</li>
<li></li>
<li>
<ol>
<li>
<p>Understanding class_exists() and interface_exists()</p>
</li>
</ol>
</li>
<li>
<ul>
<li>
<p>class_exists(string $className, bool $autoload = true): Checks if the specified class has been defined.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>interface_exists(string $interfaceName, bool $autoload = true): Checks if the specified interface has been defined.</p>
</li>
</ul>
</li>
<li></li>
<li>
<p>Note: class_exists only checks classes, interface_exists only checks interfaces. Using class_exists alone cannot directly determine if a class implements a specific interface.</p>
</li>
<li></li>
<li>
<ol start="2">
<li>
<p>Checking if a class implements an interface</p>
</li>
</ol>
</li>
<li>
<p>In PHP, there are several ways to check if a class implements an interface:</p>
</li>
<li></li>
<li>
<p>Method 1: Using class_implements()</p>
</li>
<li>
<p>class_implements($className) returns an array of all interfaces implemented by the class.<br>
*/</p>
</li>
</ul>
<p>interface LoggerInterface {<br>
public function log(string $message);<br>
}</p>
<p>class FileLogger implements LoggerInterface {<br>
public function log(string $message) {<br>
echo "Logging to file: $message";<br>
}<br>
}</p>
<p>$className = 'FileLogger';<br>
$interfaceName = 'LoggerInterface';</p>
<p>if (class_exists($className) && interface_exists($interfaceName)) {<br>
$interfaces = class_implements($className);<br>
</span>if (in_array($interfaceName, </span>$interfaces)) {<br>
echo "$className has implemented interface $interfaceName";<br>
} else {<br>
echo "$className has not implemented interface $interfaceName";<br>
}<br>
} else {<br>
echo "Class or interface does not exist";<br>
}</p>
<p>/**</p>
<ul>
<li>
<p>Method 2: Using instanceof</p>
</li>
<li>
<p>You can also instantiate the object and use instanceof to check:<br>
*/</p>
</li>
</ul>
<p>$logger = new FileLogger();<br>
if ($logger instanceof LoggerInterface) {<br>
echo "<br>Using instanceof, this object implements LoggerInterface";<br>
}</p>
<p>/**</p>
<ul>
<li>
<p>Summary:</p>
</li>
<li>
<ol>
<li>
<p>class_exists() only checks if a class exists.</p>
</li>
</ol>
</li>
<li>
<ol start="2">
<li>
<p>interface_exists() checks if an interface exists.</p>
</li>
</ol>
</li>
<li>
<ol start="3">
<li>
<p>To determine if a class implements an interface, use class_implements() to get the array of interfaces, then check with in_array().</p>
</li>
</ol>
</li>
<li>
<ol start="4">
<li>
<p>You can also use instanceof on an object instance.</p>
</li>
</ol>
</li>
<li></li>
<li>
<p>This allows us to safely check at runtime whether a class has implemented an interface, avoiding errors from calling unimplemented methods.<br>
*/<br>
?></p>
</li>
</ul>
<p><hr></p>
<p data-is-last-node="" data-is-only-node=""><?php<br>
// Footer unrelated to the main content<br>
echo "Thank you for reading this PHP tutorial!<br>";<br>
$footer_note = "Today we learned how to use class_exists, interface_exists, and class_implements.";<br>
echo $footer_note;<br>
?><br>
</span>