> I have a need for a package mechanism like that provided by TCL. We
> write a
> lot of code in reusable libraries and often encounter the situation in
> which
> an application uses a particular library but requires at least version X
> or
> greater.
>
> The require(), require_once(), etc. functionality doesn't provide any
> version
> checking facility and I'm not aware of any other such mechanism built
> into
> PHP.
>
> I played around with the following which kind of emulates the TCL
> behaviour:
> In each library I set a variable that contains that file's version
> number
> (automatically populated by CVS keyword expansion). I then replaced my
> require() statements by a customised version which takes version number
> arguments.
>
> So, in my application script I have, for example:
>
> require_once("package.inc");
> package_require("libraryfile.inc", 1.5, 0);
> ...
>
> And in package.inc I have:
>
> function package_require($file, $minVersion, $maxVersion) {
>
> require_once($file);
>
> // Code to check if $file is within the specified bounds
>
> }
>
>
> The obvious (potential) problems with this approach are with respect to
> scope. Most of the libraries I use contain function and class
> definitions and
> I'm not at all sure about the consequences of declaring these from
> within
> another function (package_require()). To my surprise, though, it seemed
> to
> work. No errors were generated and I could happily use the classes and
> functions as usual. I'm a little nervous, though, about problems that
> are not
> immediately visible.
>
> Secondly, there is the problem of global variables. Any variables set in
> the
> global scope in the library files are now in the local scope of the
> package_require() function. I cannot put 'global' declarations for each
> of
> these as I would have to know about each and every one of them in
> advance
> which prevents it from being a general solution.
>
> Does anyone else have some good ideas about this issue?
> Are there any plans to implement this in PHP itself where it properly
> belongs?



Douw,

As to the first question: if package_require($file, $minVersion, $maxVersion) was set 
up to only return the assembled
filename (not perform the require call), and the require_once($file) nested that 
function call and was placed in the
'mainline', would that allay some of your concerns?

     require_once( package_require( $file [, args ] ) );

- but what if there isn't a suitable candidate? Would there be some advantage to 
setting package_require() to return a
boolean (suitable include file found - success/failure) and then:

     package_require( $file [, args ] ) ? require_once( $file ) : die( 'or whatever' );

which 'blends' quite happily into a list of require_once() calls - but could be an 
if-then-else construct if you prefer
that presentation.

As to the last question, I can't say, but I was recently asking for a tidy method to 
keep development/test and
production code/includes/databases separate and ended up writing my own name-handler, 
much as yours...

Regards,
=dn



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to