Load extension classes

Im trying to write a site, where I want to be able to add extensions into it at a later date. I want to loop over a folder and include each file in the folder which I can do easily. My problem is how to list all available classes that Ive loaded.

So what Ive come up with is a simple test which loads all available classes before, then reads a class, reads all available classes and then looks at the 2 arrays and displays the differences which should be just my class:-
<?php
  $startupClasses = get_declared_classes();

  class testMe {
    public function __construct() {

    }
  }

  $endClasses = get_declared_classes();

  $availableClasses = array_diff($startupClasses,$endClasses);

  print_r($availableClasses);

?>

Open in new window


However all I get is an empty array back.

Anyone got any ideas how to fix this, or if anyone has any ideas how I can simply load extensions by putting files into a folder?

Thanks in advance
tonelm54Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
This will probably be what you want.
http://php.net/manual/en/language.oop5.autoload.php
http://php.net/manual/en/function.autoload.php
http://php.net/manual/en/function.spl-autoload-register.php

For performance reasons, I have avoided using autoload().  I find that it is better for performance to load all of the class definitions in a single "common.php" file.  Your build process can compose the common script from your development or deployment libraries.  Fewer disk read operations means shorter time to load the scripts and render the web pages.
0
tonelm54Author Commented:
I have looked at these, but there isn't a way to list what classes are loaded.

What I want to do is have a page which loads all the available files from the directory then list the classes. In each of the classes have a function for description, settings etc so the user can setup the class when required.

What I want to be able to do is just drop the class file into the directory and then to be able to list the classes it and other included files load.
0
Ray PaseurCommented:
Classes are "declared" and "loaded" in separate actions.  The PHP parser knows of every declared class before it begins to run the script.  In other words, the location of the class definition is irrelevant in this example.  See:
http://www.laprbass.com/RAY_temp_tonelm54.php

<?php // RAY_temp_tonelm54.php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
echo '<pre>';

$startupClasses = get_declared_classes();

class testMe
{
    public function __construct() { }
}

$endClasses = get_declared_classes();
$availableClasses = array_diff($startupClasses,$endClasses);

// SHOW WHAT WE HAVE FOUND
var_dump($startupClasses, $endClasses, $availableClasses);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
I think (though I have not tested any) that you may find solutions in the user-contributed notes here.  There seem to be some fairly strict file naming conventions, and that is probably good!
http://php.net/manual/en/function.spl-autoload-register.php
0
Ray PaseurCommented:
Thanks for the points.  I'd be interested to know what output you get when you run that script.  If you would be so kind as to copy the var_dump() output and paste it into the code snippet, it would be really helpful to me.  Thanks, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.