if (!class_exists("classname")) {
...your class here...
}
One possibility:
file1.php includes: file2.php, file3.php
file2.php includes: file3.php
Which would mean file1.php sees file3.php twice, and thus possibly tries to redeclare a class.
with the structure I used above to ensure no name collision.
I wouldn't use that - if I make a duplicate class, it's an accident; that ensures that my mistake is harder to track down...
I'm not saying you're "wrong", but asking you to expand your mind and realize these PHP commands do have a purpose in professional development and you should start using them as you advance in the OOP/PHP world.
if (!class_exists("CLASSNAME")) {
...your class here...
} else {
throw new Exception('CLASSNAME already defined. Duplicate in ' . __FILE__);
}
@NoNameBrand
First, you said: "I wouldn't use that"
Then, you said: " I do use the class/function existence functions in my own work"
As your code has a "throw" in it, you are not using the handler which is really more efficient in a larger project (as I said previously).
trigger_error('foo');
In the world of OOP which is not often discussed here it is common for developers to share classes and functions written by others as well as extend classes including those beyond just your own. In addition, a major component of quality development is error handling and parsing at runtime which catches such errors gracefully (i.e. an error triggered by calling a method of the wrong object in a dupe class name scenario). You cannot possibly test for all failure scenarios when the code starts getting complex.
I understand each of your positions in the context in which you're both programming.
As to the function, my statement involved a different context, I think a very real and common situation where other classes and extensions of classes are involved. Just as an FYI, I also wish to add that function is very helpful to ensure a class was invoked properly via a plugin scenario where a user might extend your code externally. Or you want to see if the class has been properly de-constructed as part of memory cleanup. Matter of fact in PHP 5.0.0 they actually added a new parameter to support __autoloading, which I think is quite convenient when used properly and clearly demonstrates programmers were using the function.
I'm not asking either of you to change your style, but merely to be open to possibilities and situations others have experienced where they find the function quite useful. As to the user defined global error handler, I can't disagree about the bigger problem part - but there is no tool that can account for all failure scenarios in all projects all the time. So adding in a decent handler and using throws or triggers along with a few sensible safeguards such as this function will all work together making a complete end product. I know you each have different ideas and methods, that's cool.
I suggest you tell Zend to deprecate the command if you see no use for it with the same justifications you explained to me.
Thanks for expressing your opinions and thanks for listening to mine. I'm moving on to other topics, I'll leave the last word to anyone who wishes to do so. Cheers.
-jim