Re: Coding standards - inclusion

From: Date: Tue, 13 Oct 2009 09:57:06 +0000
Subject: Re: Coding standards - inclusion
References: 1 2 3  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-52935@lists.php.net to get a copy of this message
Hello Eric, > >> From > >> http://pear.php.net/manual/en/standards.including.php: > >> "Anywhere you are unconditionally including a class file, use > >> require_once. Anywhere you are conditionally including a class file > >> (for example, factory methods), use include_once." > >> > >> Out of curiosity, why is this the case? What benefits does it > >> offer? > > > > A failed require leads to a fatal error which is not catchable by > > code. In you library, you should include a file and throw an > > exception if that failed - this is programmatically verifiable. > > Thank-you for the response, and good point. > > Is there some recommended way of checking success? I thought of this: > > if (@include 'test.php') > echo 'yes'; > else > throw new Exception('no'); I would not do that - especially during development, a syntax error in test.php would be silenced and you'd stare at a white page without knowing anything. > Some say the error control operator is quite slow; is there some > advantage to the above vs: > > if (file_exists('test.php')) > require 'test.php'; > else > throw new Exception('no'); That one does not work with files in your include path. I'd include the file without any file_exist checks. If you really need to know if your file is includable, use fopen with the search-include-path option on (3rd parameter). After inclusion, you can do a class_exists check to see if the class you expected is available. -- Mit freundlichen Grüßen Christian Weiske

« previous php.pear.dev (#52935) next »