note 101545 added to function.class-exists

From: Date: Thu, 23 Dec 2010 16:25:47 +0000
Subject: note 101545 added to function.class-exists
Groups: php.notes 
Request: Send a blank email to php-notes+get-175174@lists.php.net to get a copy of this message
Declare a class only if does not exists. ------------------Example 1------------------ <?php if(!class_exists('MyClass')) { class MyClass { public function __construct() { echo "<br />Hello<br />"; } } } $x = new MyClass(); ?> Output: Hello ------------------Example 2------------------ <?php class MyClass { public function __construct() { echo "<br />Hi<br />"; } } if(!class_exists('MyClass')) { class MyClass { public function __construct() { echo "<br />Hello<br />"; } } } ?> Output: Hi ------------------Usage------------------ If any class is not supported in older version of PHP you can check it before creating it. Like PHP version less than 5.1 does not support InvalidArgumentException class, So before using it you can make sure it does exists but using class_exists() <?php if(!class_exists(InvalidArgumentException)) { class InvalidArgumentException extends Exception { } } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 117.254.255.36 ---- Manual Page -- http://www.php.net/manual/en/function.class-exists.php Edit -- https://master.php.net/note/edit/101545 Del: integrated -- https://master.php.net/note/delete/101545/integrated Del: useless -- https://master.php.net/note/delete/101545/useless Del: bad code -- https://master.php.net/note/delete/101545/bad+code Del: spam -- https://master.php.net/note/delete/101545/spam Del: non-english -- https://master.php.net/note/delete/101545/non-english Del: in docs -- https://master.php.net/note/delete/101545/in+docs Del: other reasons-- https://master.php.net/note/delete/101545 Reject -- https://master.php.net/note/reject/101545 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#175174) next »