Page 1 of 1

Creating an unknown object...

Posted: Sat Feb 03, 2007 3:50 pm
by alex.barylski
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:

Code: Select all

MyClass1
MyClass2
_afxMyClass3
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? :P

Cheers :)

Posted: Sat Feb 03, 2007 3:56 pm
by Ollie Saunders
An extract from SimpleTest might help you:

Code: Select all

/**
 *    Builds a group test from a library of test cases.
 *    The new group is composed into this one.
 *    @param string $test_file        File name of library with
 *                                    test case classes.
 *    @access public
 */
function addTestFile($test_file) {
    $existing_classes = get_declared_classes();
    if ($error = $this->_requireWithError($test_file)) {
        $this->addTestCase(new BadTestSuite($test_file, $error));
        return;
    }
    $classes = $this->_selectRunnableTests($existing_classes, get_declared_classes());
    if (count($classes) == 0) {
        $this->addTestCase(new BadTestSuite($test_file, "No runnable test cases in [$test_file]"));
        return;
    }
    $group = &$this->_createGroupFromClasses($test_file, $classes);
    $this->addTestCase($group);
}
/**
 *    Calculates the incoming test cases from a before
 *    and after list of loaded classes. Skips abstract
 *    classes.
 *    @param array $existing_classes   Classes before require().
 *    @param array $new_classes        Classes after require().
 *    @return array                    New classes which are test
 *                                     cases that shouldn't be ignored.
 *    @access private
 */
function _selectRunnableTests($existing_classes, $new_classes) {
    $classes = array();
    foreach ($new_classes as $class) {
        if (in_array($class, $existing_classes)) {
            continue;
        }
        if ($this->_getBaseTestCase($class)) {
            $reflection = new SimpleReflection($class);
            if ($reflection->isAbstract()) {
                SimpleTest::ignore($class);
            }
            $classes[] = $class;
        }
    }
    return $classes;
}

Posted: Sat Feb 03, 2007 4:19 pm
by alex.barylski
Didn't even think of SimpleTest...but yes thats basically exactly what I had in mind and seeing someone else use that technique successfully is all the confirmation I need, that the technique is usable.

Cheers :)

Re: Creating an unknown object...

Posted: Sat Feb 03, 2007 11:43 pm
by Christopher
Hockey wrote: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.
Let me get this straight ... you have class names like "Some_Class_Name" using that fairly horrid, but very practical PEAR naming style -- but you don't want to name the file containing that class "Some/Class/Name.php" !?!

Posted: Sun Feb 04, 2007 12:01 am
by alex.barylski
That is correct - I stand my ground on that argument to this day.

It favours convenience & convention over mobility. I don't know, nor do I care if your file structure once layed out stays concrete - mine don't.

I constantly refactor everything in an application and I am always seeking ways to better the organization of my project at all levels, not just source.