mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 08:58:56 +00:00
Added simple example
Cleaned initial example Added links to autoload git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@169150 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
30f7de760e
commit
7276e35197
1 changed files with 43 additions and 25 deletions
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
<!-- $Revision: 1.5 $ -->
|
||||
<!-- $Revision: 1.6 $ -->
|
||||
<!-- splitted from ./en/functions/classobj.xml, last change in rev 1.11 -->
|
||||
<refentry id="function.class-exists">
|
||||
<refnamediv>
|
||||
|
@ -8,26 +8,37 @@
|
|||
</refnamediv>
|
||||
<refsect1>
|
||||
<title>Description</title>
|
||||
<methodsynopsis>
|
||||
<type>bool</type><methodname>class_exists</methodname>
|
||||
<methodparam><type>string</type><parameter>class_name</parameter></methodparam>
|
||||
<methodparam choice="opt"><type>bool</type><parameter>autoload</parameter></methodparam>
|
||||
</methodsynopsis>
|
||||
<note>
|
||||
<para>
|
||||
The <parameter>autoload</parameter> parameter was added in PHP 5
|
||||
</para>
|
||||
</note>
|
||||
<methodsynopsis>
|
||||
<type>bool</type><methodname>class_exists</methodname>
|
||||
<methodparam><type>string</type><parameter>class_name</parameter></methodparam>
|
||||
<methodparam choice="opt"><type>bool</type><parameter>autoload</parameter></methodparam>
|
||||
</methodsynopsis>
|
||||
<para>
|
||||
This function returns &true; if the class given by
|
||||
<parameter>class_name</parameter> has been defined,
|
||||
&false; otherwise.
|
||||
</para>
|
||||
<para>
|
||||
<function>class_exists</function> will attempt to call __autoload by
|
||||
default, if you don't want <function>class_exists</function> to
|
||||
call __autoload(), you can set the parameter <parameter>autoload</parameter>
|
||||
to &false;.
|
||||
<example>
|
||||
<title><function>class_exists</function> example</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
// Check the class exists before trying to use it
|
||||
if (class_exists('MyClass')) {
|
||||
$myclass = new MyClass();
|
||||
}
|
||||
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
</para>
|
||||
<para>
|
||||
<function>class_exists</function> will attempt to call &link.autoload; by
|
||||
default, if you don't want <function>class_exists</function> to
|
||||
call &link.autoload;, you can set the parameter <parameter>autoload</parameter>
|
||||
to &false;.
|
||||
</para>
|
||||
<para>
|
||||
<example>
|
||||
|
@ -35,26 +46,33 @@
|
|||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
function __autoload($class) {
|
||||
include($class . '.php');
|
||||
function __autoload($class)
|
||||
{
|
||||
include($class . '.php');
|
||||
|
||||
/* Check to see it the include defined the class */
|
||||
if ( !class_exists($class, false) ) {
|
||||
trigger_error("Unable to load class $class", E_USER_ERROR);
|
||||
}
|
||||
// Check to see if the include declared the class
|
||||
if (!class_exists($class, false)) {
|
||||
trigger_error("Unable to load class: $class", E_USER_WARNING);
|
||||
}
|
||||
}
|
||||
|
||||
if( class_exists('MyClass') ) {
|
||||
$myclass = new MyClass();
|
||||
}
|
||||
if (class_exists('MyClass')) {
|
||||
$myclass = new MyClass();
|
||||
}
|
||||
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
</para>
|
||||
<note>
|
||||
<para>
|
||||
The <parameter>autoload</parameter> parameter was added in PHP 5
|
||||
</para>
|
||||
</note>
|
||||
<simpara>
|
||||
See also <function>get_declared_classes</function>.
|
||||
See also <function>interface_exists</function>, and
|
||||
<function>get_declared_classes</function>.
|
||||
</simpara>
|
||||
</refsect1>
|
||||
</refentry>
|
||||
|
|
Loading…
Reference in a new issue