Click to See Complete Forum and Search --> : My implementation of import


Moonglobe
02-06-2004, 02:18 AM
This is how i implemented import and namespaces in PHP. obviously this is *very* limited, as its not part of the language, but it does provide a simple way of organizing classes. Now if you were really tricky and were using PHP 5 (as i am), you could mess with your naming sceme and __autoload() to get the same results with one less user-typed function call, but i think import looks cooler ;).

In any case, i know it may seem kind of boring, but after reading a post about something like this implemented in the engine, i decided to write it to be as extensible as possible (set the first param to false and you have 3 init options: base directory, namespace delimeter character, and error out level (that's some code ripped from the token_name() user notes)).

enjoy another pointless piece of code by Moonglobe :)

Forgot a few things, and also added support for single file inclusion without a one-file directory. here's what i forgot to say:

if any of the included files spit anything out, then the function checks if there is "error" (case-insensitive) in the text. if so, it lets it show up, if not, it cuts it (this is because im often too lazy to take out the debugging code at the bottom of my class definition scripts :p). Also now, if there's not a directory that matches the namespace, the last namespace has '.php' tacked on and there's a check to see if that file exists. This functionality is turned off by default though, you have to pass true to the 5th param during init to get that operational.

Weedpacket
02-06-2004, 06:24 AM
I'm gonna have to have a look at this, might be better than what I'm bashing on; er... usage dox? Comments? v5.2?

Moonglobe
02-06-2004, 11:31 AM
haha sorry, that'sonly the do_nothing( function. there are a lot of bug fixes im putting in right now (i messed up my error code, and the single file stuff totally wrecked everything else). I'll post docs and a fixed, commented version up later today.

drawmack
02-06-2004, 03:02 PM
Originally posted by Moonglobe
this is because im often too lazy to take out the debugging code at the bottom of my class definition scripts

you should work with assertions cause they can be gloabally turned off by setting a constant.

Moonglobe
02-06-2004, 07:32 PM
drawmack, perhaps you could explain that for me. i've never quite got around the assert() statement and what it does...

Or just point me at a link... and not the manual i've read that almost cover-to-cover twice. ;)

Weedpacket
02-07-2004, 12:11 AM
Originally posted by Moonglobe
haha sorry, that'sonly the do_nothing() function. Jest proving I read it :). I've got a pile of functions I'm slowly plugging away at (interrelated selections of RNGs, random distributions, statistical functions, and special functions) and having a smarter namespace mechanism might come in handy.

Moonglobe
02-09-2004, 01:26 AM
ok so here's what i hope to be a working version... still not enough commenting, but it will get there :)