Creating an unknown object...
Posted: Sat Feb 03, 2007 3:50 pm
Sounds crazy doesn't...
Heres the problem and proposed solution:
Based on inclusion of a file I need to instantiate the class which is contained inside that file but I do not wish to force any convention on class names or files, so...
I figure what I can do is take a snapshop of declared classes before and after inclusion of the file, do a quick compare and determine the class which has just been included based on that.
Of course, the first question becomes "What happens if more than a single class get included - via another include, etc?"
Here is where I may consider something of a convention, like maybe ONLY instantiate a class whose name starts with "_afx" or whatever prefix I come up with...
That way, if three classes are included after initial snapshop:
Only the last snapshot will be created...
I know I could just following a more strict convention, such as class.myclass3.php would map directly to MyClass3 which would then be created, but ideally I would like to avoid that strict of a convention.
My reasoning is simple: I have class names like (Some_Class_Name; see Zend) whereas my file system is always lowercase and is usually abbreviated. But enough of that, this is purely personal preference.
What I would like to hear is a better way of implementing the technique I have described above? Maybe even an implementation?
I am guessing some of you might be tempted to just direct me to the function reference (array_diff, etc) but thats not entirely what I'm looking for...i would like something a little more indepth (otherwise I would have posted in PHP code)...
Hopefully I've made sense?
Cheers
Heres the problem and proposed solution:
Based on inclusion of a file I need to instantiate the class which is contained inside that file but I do not wish to force any convention on class names or files, so...
I figure what I can do is take a snapshop of declared classes before and after inclusion of the file, do a quick compare and determine the class which has just been included based on that.
Of course, the first question becomes "What happens if more than a single class get included - via another include, etc?"
Here is where I may consider something of a convention, like maybe ONLY instantiate a class whose name starts with "_afx" or whatever prefix I come up with...
That way, if three classes are included after initial snapshop:
Code: Select all
MyClass1
MyClass2
_afxMyClass3I know I could just following a more strict convention, such as class.myclass3.php would map directly to MyClass3 which would then be created, but ideally I would like to avoid that strict of a convention.
My reasoning is simple: I have class names like (Some_Class_Name; see Zend) whereas my file system is always lowercase and is usually abbreviated. But enough of that, this is purely personal preference.
What I would like to hear is a better way of implementing the technique I have described above? Maybe even an implementation?
I am guessing some of you might be tempted to just direct me to the function reference (array_diff, etc) but thats not entirely what I'm looking for...i would like something a little more indepth (otherwise I would have posted in PHP code)...
Hopefully I've made sense?
Cheers