<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Return the traits used by the given class</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.class-parents.html">class_parents</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.iterator-apply.html">iterator_apply</a></div> <div class="up"><a href="ref.spl.html">SPL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.class-uses" class="refentry"> <div class="refnamediv"> <h1 class="refname">class_uses</h1> <p class="verinfo">(PHP 5 >= 5.4.0)</p><p class="refpurpose"><span class="refname">class_uses</span> — <span class="dc-title"> Return the traits used by the given class </span></p> </div> <div class="refsect1 description" id="refsect1-function.class-uses-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>class_uses</strong></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$class</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$autoload</code><span class="initializer"> = true</span></span> ] )</div> <p class="para rdfs-comment"> This function returns an array with the names of the traits that the given <em><code class="parameter">class</code></em> uses. This does however not include any traits used by a parent class. </p> </div> <div class="refsect1 parameters" id="refsect1-function.class-uses-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">class</code></em></span> <dd> <p class="para"> An object (class instance) or a string (class name). </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">autoload</code></em></span> <dd> <p class="para"> Whether to allow this function to load the class automatically through the <span class="function"><a href="function.autoload.html" class="function">__autoload()</a></span> magic method. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.class-uses-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> An array on success, or <strong><code>FALSE</code></strong> on error. </p> </div> <div class="refsect1 examples" id="refsect1-function.class-uses-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-3914"> <p><strong>Example #1 <span class="function"><strong>class_uses()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />trait foo </span><span style="color: #007700">{ }<br />class </span><span style="color: #0000BB">bar </span><span style="color: #007700">{<br /> use </span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">class_uses</span><span style="color: #007700">(new </span><span style="color: #0000BB">bar</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">class_uses</span><span style="color: #007700">(</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">));<br /><br />function </span><span style="color: #0000BB">__autoload</span><span style="color: #007700">(</span><span style="color: #0000BB">$class_name</span><span style="color: #007700">) {<br /> require_once </span><span style="color: #0000BB">$class_name </span><span style="color: #007700">. </span><span style="color: #DD0000">'.php'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// use __autoload to load the 'not_loaded' class<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">class_uses</span><span style="color: #007700">(</span><span style="color: #DD0000">'not_loaded'</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Array ( [foo] => foo ) Array ( [foo] => foo ) Array ( [trait_of_not_loaded] => trait_of_not_loaded ) </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.class-uses-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.class-parents.html" class="function" rel="rdfs-seeAlso">class_parents()</a> - Return the parent classes of the given class</span></li> <li class="member"> <span class="function"><a href="function.get-declared-traits.html" class="function" rel="rdfs-seeAlso">get_declared_traits()</a> - Returns an array of all declared traits</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.class-parents.html">class_parents</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.iterator-apply.html">iterator_apply</a></div> <div class="up"><a href="ref.spl.html">SPL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>